Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is C# truncating "00" to "0" when using ToString() method

I am trying to create a unique value using a day,month,hour,minute and seconds. if i create the unique value at "2022/10/10 15:00:00" and i use ToString(), the "00" are truncated to "0"

var today = DateTime.Now;
string key = string.Format("341{0}{1}{2}{3}{4}", today.Date.Day, today.ToString("MM"), today.Hour.ToString(), today.Minute.ToString(), today.Second.ToString());

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s not 00 value. It’s DateTime struct with int properties.

Use interpolation and proper DateTime format:

string key = $"341{today:ddMMHHmmss}"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading