简体   繁体   中英

Converting DateTime to EPOCH time is returning a new Date

To convert date time to EPOCH time , I am using following function:

public static long EpochTime(DateTime dt)
        {
            //long form code to be clear
            TimeSpan t = dt.ToLocalTime() - new DateTime(1970, 1, 1);
            long millisecondsSinceEpoch = (long)t.TotalSeconds * 1000;
            return millisecondsSinceEpoch;
        }

Now If I test the function with input date as , 15/07/2018 1:09:42 PM , the out put date becomes ,

GMT: Sunday, July 15, 2018 11:09:42 PM
Your time zone: Monday, July 16, 2018 9:09:42 AM GMT+10:00

I need the epoch time as Highchart needs epoch time on x-axis.

You need to ensure that you're comparing times in the correct timezones. I recommend converting both to UTC:

TimeSpan t = dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM