get timestamp in seconds from datetime in c#

Smith
Smith
Teacher
714 Points
152 Posts

I want to get timestamp so that we can do comparison on it as numeric comparison instead of data comparison.

Views: 179
Total Answered: 1
Total Marked As Answer: 1
Posted On: 07-Mar-2022 05:42

Share:   fb twitter linkedin
Answers
Priya
Priya
Member
220 Points
50 Posts
         

Use DateTimeOffset.ToUnixTimeSeconds method to get time stamp. It will returns the number of seconds that have elapsed since 1970-01-01T00:00:00Z.

To get the timestamp from a DateTime:

DateTime foo = DateTime.Now;
long unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds();

For more detail:

https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimeseconds?view=net-6.0

 

Posted On: 16-Mar-2022 23:44
thanks.
 - Smith  20-Apr-2022 23:54
 Log In to Chat