Convert DateTime.Now to Seconds

90,792

Solution 1

The DateTime type supports comparison operators:

if (dateTimeA > dateTimeB)
{
    ...

This also works for DateTime values returned by DateTime.AddSeconds:

if (dateTimeA.AddSeconds(42) > dateTimeB)
{
    ...

If you really want the number of seconds that elapsed since 01/01/0001 00:00:00, you can calculate the difference between the two DateTime values. The resulting TimeSpan value has a TotalSeconds property:

double result = DateTime.Now.Subtract(DateTime.MinValue).TotalSeconds;

Solution 2

It really doesn't make sense to convert a DateTime object to seconds. Seconds only make sense if you are dealing with a length of time (TimeSpan). Should you want to compare two dates to get the number of seconds between them:

TimeSpan diff = DateTime.Now - PreviousDateTime;
double seconds = diff.TotalSeconds;

Solution 3

If the purpose is finding the number of seconds between two dates, you'd be much better off using the TimeSpan object.

TimeSpan span = date2 - date1;
double seconds = span.TotalSeconds;

Solution 4

See suggestion from thread below:

How do I convert ticks to minutes?

TimeSpan.FromTicks(DateTime.Now.Ticks).TotalSeconds; 

Solution 5

Assuming you really need to get at the seconds for the datetime object, you could directly get the "Ticks" property from it. These aren't in seconds but you can easily divide by the proper factor to convert the Ticks to seconds. See: http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx

So, something like:

        DateTime.Now.Ticks/TimeSpan.TicksPerSecond
Share:
90,792
TheDevOpsGuru
Author by

TheDevOpsGuru

I am a Java and .NET developer who has migrated into a Software Engineering / DevOps / TestOps role. I also have strong skills in SQL.

Updated on July 09, 2022

Comments

  • TheDevOpsGuru
    TheDevOpsGuru almost 2 years

    I am trying to write a function that will convert a DateTime.Now instance to the number of seconds it represents so that I can compare that to another DateTime instance. Here is what I currently have:

    public static int convertDateTimeToSeconds(DateTime dateTimeToConvert)
        {
            int secsInAMin = 60;
            int secsInAnHour = 60 * secsInAMin;
            int secsInADay = 24 * secsInAnHour;
            double secsInAYear = (int)365.25 * secsInADay;
    
            int totalSeconds = (int)(dateTimeToConvert.Year * secsInAYear) + 
                           (dateTimeToConvert.DayOfYear * secsInADay) +
                           (dateTimeToConvert.Hour * secsInAnHour) +
                           (dateTimeToConvert.Minute * secsInAMin) + 
                           dateTimeToConvert.Second;
    
            return totalSeconds;
        }
    

    I realize that I am truncating the calculation for seconds in a year, but I don't need my calculation to be precise. I'm really looking to know if the method that I am using to calculate seconds is correct.

    Does anyone have anything that could better compute seconds given from a DateTime object?

    Also, Should the return type be int64 if I am coding in C# if I am going to calculate all the seconds since 0 AD?