// local times DateTime creationTime = File.GetCreationTime(@"c:\file.txt"); DateTime lastWriteTime = File.GetLastWriteTime(@"c:\file.txt"); DateTime lastAccessTime = File.GetLastAccessTime(@"c:\file.txt"); // UTC times DateTime creationTimeUtc = File.GetCreationTimeUtc(@"c:\file.txt"); DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(@"c:\file.txt"); DateTime lastAccessTimeUtc = File.GetLastAccessTimeUtc(@"c:\file.txt"); // write file last modification time (local / UTC) Console.WriteLine(lastWriteTime); // 9/30/2007 2:16:04 PM Console.WriteLine(lastWriteTimeUtc); // 9/30/2007 6:16:04 PM // FileInfo ile FileInfo fileInfo = new FileInfo(@"c:\file.txt"); // local times DateTime creationTime = fileInfo.CreationTime; DateTime lastWriteTime = fileInfo.LastWriteTime; DateTime lastAccessTime = fileInfo.LastAccessTime; // UTC times DateTime creationTimeUtc = fileInfo.CreationTimeUtc; DateTime lastWriteTimeUtc = fileInfo.LastWriteTimeUtc; DateTime lastAccessTimeUtc = fileInfo.LastAccessTimeUtc; // write file last modification time (local / UTC) Console.WriteLine(lastWriteTime); // 9/30/2007 2:16:04 PM Console.WriteLine(lastWriteTimeUtc); // 9/30/2007 6:16:04 PM
Ref: http://www.csharp-examples.net/file-creation-modification-time/
Stackoverflow'dan DateTime tür dönüşümleri üzerine bir cevap
Given that you are parsing the date from a string, the system has no way of knowing what kind of DateTime you are parsing.DateTime has a Kind property, which can have one of the three values:
Unspecified
Local
Utc
So for the code you show:
DateTime convertedDate = DateTime.Parse(dateStr); var kind = convertedDate.Kind; // will equal DateTimeKind.Unspecified You say you know what kind it is, so tell it. DateTime convertedDate = DateTime.SpecifyKind( DateTime.Parse(dateStr), DateTimeKind.Utc);var kind = convertedDate.Kind; // will equal DateTimeKind.Utc
Now, once the system knows its in UTC time, you can just call ToLocalTime:
DateTime dt = convertedDate.ToLocalTime();This will give you the result you require.
Hiç yorum yok:
Yorum Gönder