简体   繁体   中英

Convert datetime ticks(String.Format(“{0:D19}”, DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks) to datetime

I am storing Azure table storage rowkey in date time ticks format

String.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);

and I want to convert back to date time.

If you are doing:

string str = String.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);

Then we have that

// str = max - now

so then

// now = max - str

so

DateTime now = new DateTime(DateTime.MaxValue.Ticks - long.Parse(str), DateTimeKind.Utc);

Your code returns 2519440974768899656 on my timezone which is UTC +02:00 right now, you can parse this string to long and can use in DateTime(long) constructor.

var s = String.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).Dump();
long l = long.Parse(s);
(new DateTime(l)).Dump();

Returns

16.10.7984 15:31:16

But honestly, depending what you doing, I'm not quite sure this is the value you want.

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