Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ToString pattern to preserve even the smallest 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 part of DateTime? How to save it to string as well?

like image 244
SharpAffair Avatar asked Dec 19 '25 05:12

SharpAffair


2 Answers

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.

like image 81
Paweł Dyda Avatar answered Dec 21 '25 00:12

Paweł Dyda


Yes, ticks represent the smallest measurement on a DateTime.

String filename = DateTime.Now.Ticks + ".txt";
like image 42
Aaron McIver Avatar answered Dec 21 '25 01:12

Aaron McIver