简体   繁体   中英

C# DateTime datebase export in Excel sheet

I am struggeling with the correct display of dates in database exports. In a xamarin application (C#) birthdates are stored in a local database using the following method.

public static long GetTimestamp (DateTime dateTime)
{
    TimeSpan timeSpan = dateTime - new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);;
    return (long)timeSpan.TotalMilliseconds;
}

and the other way around:

public static DateTime GetDateTime (long milliseconds)
{
    DateTime start = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return start.AddMilliseconds (milliseconds).ToUniversalTime ();
}

If I look into the database, negative values like eg -650073600000 are stored in the database.

In the exported excel sheets the birthdates are displayed as those values as well. I am using Excel for Mac 2011.

How can I convert those values to be displayed as the proper dates in Excel?

Thank you for any help.

If Excel formula, you can do this:

=DATE(1970,1,1)+(A2/(1000*60*60*24))

(In this example, cell A2 contains the value you need to convert).

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