English 中文(简体)
将DateTime转换为long,也可以转换为其他方式
原标题:Convert DateTime to long and also the other way around

我想把日期作为数字存储在表格中。我知道该怎么做,但我不知道该怎么回去。如何将长变量强制转换为ToDateTime。

DateTime now = DateTime.Now;
long t = now.ToFileTime();
DateTime today = t.ToDateTime;  // I am looking for something like this line. This Method does not exist

我知道有很多方法可以将DateTime转换为long。我不介意用哪种技术。我只想有一种可以来回转换的方式。

最佳回答

要从DateTime延长:

长日期时间。Ticks

到日期从长开始的时间:

新日期时间(长)

问题回答

从长到日期时间:新日期时间(长刻度)

从DateTime到long:DateTime.Ticks

由于您使用的是ToFileTime,因此您需要使用FromFileTime走另一条路。但是注意

Ordinarily, the FromFileTime method restores a DateTime value that was saved by the ToFileTime method. However, the two values may differ under the following conditions:

If the serialization and deserialization of the DateTime value occur in different time zones. For example, if a DateTime value with a time of 12:30 P.M. in the U.S. Eastern Time zone is serialized, and then deserialized in the U.S. Pacific Time zone, the original value of 12:30 P.M. is adjusted to 9:30 A.M. to reflect the difference between the two time zones.

If the DateTime value that is serialized represents an invalid time in the local time zone. In this case, the ToFileTime method adjusts the restored DateTime value so that it represents a valid time in the local time zone.

如果您不关心DateTime的哪种表示形式被存储,您可以按照其他人的建议使用TicksTicks可能更可取,具体取决于您的要求,因为ToFileTime返回的值似乎在Windows文件系统API的上下文中)。

有几种可能性(请注意,这些长值和Unix epoch不同。

对于您的示例(要反转ToFileTime()),只需使用DateTime.FromFileTime(t)

有一个DateTime构造函数需要很长的时间。

DateTime today = new DateTime(t); // where t represents long format of dateTime 
   long dateTime = DateTime.Now.Ticks;
   Console.WriteLine(dateTime);
   Console.WriteLine(new DateTime(dateTime));
   Console.ReadKey();

如果要使用1970年以来的秒数而不是Ticks:

UTC:

long secondsSince1970 = DateTimeOffset.Now.ToUnixTimeSeconds();

当地时间:

long secondsSince1970 = DateTime.Now.Ticks / 10000000 - 62135596800;

回到DateTime

DateTime value = DateTime.MinValue.AddSeconds(secondsSince1970).AddYears(1969);

我将添加一些从字符串到DateTime到long的额外转换

string timeString = "2016-08-04";
DateTime date = DateTime.Parse(timeString);
long dateTime = date.Ticks;

这里有一种简写的说法:

long dateTime = DateTime.Parse("2016-08-04").Ticks;

//And for the long to DateTime

DateTime date = new DateTime(dateTime);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...