开发者

DateTime.ToString pattern to preserve even the smallest part

开发者 https://www.devze.com 2023-01-26 15:15 出处:网络
I need to store exact DateTime as a filename, and restore it when needed. Pattern dd_MM_yyyy hh_mm_ss_ff tt doesn\'t seem to match the original date (after using DateTime.ParseExact, so I believe part

I need to store exact DateTime as a filename, and restore it when needed. Pattern dd_MM_yyyy hh_mm_ss_ff tt doesn't seem to match the original date (after using DateTime.ParseExact, so I believe part of the date is lost upon conversion. Is ticks the smallest pa开发者_开发百科rt of DateTime? How to save it to string as well?


I believe you want to reliably time-stamp your log file. Well, the format you provide will not be reliable, i.e. it will look different depending on Thread's CurrentCulture value.

I would recommend using something like "yyyy-MM-dd_HH:mm:ss.FFFFFFF" if you want to have precise and reliable time stamp.


Yes, ticks represent the smallest measurement on a DateTime.

String filename = DateTime.Now.Ticks + ".txt";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号