Separate date and time with "(String.Format)

Is it possible to divide the date and time by ".

So this will be:

"ddMMyyyy","HHmmss" 

Now I have:

 DateTime dt = aPacket.dtTimestamp; string d = dt.ToString("\"ddMMyyyy\",\"HHmmss\""); 

and String.Format shows me only ddMMyyyy, HHmmss

Thank you all for your help! But I will mark the first answer as correct

+5
source share
4 answers

You can try formatting:

  DateTime dt = DateTime.Now; // "01072016","101511" string d = String.Format("\"{0:ddMMyyyy}\",\"{0:HHmmss}\"", dt); 
+7
source

" is a formatting character, so it must be escaped with \ , for example.

 string d = dt.ToString("\\\"ddMMyyyy\\\",\\\"HHmmss\\\""); 

You can find the shorthand line a little more readable:

 string d = dt.ToString(@"\""ddMMyyyy\"",\""HHmmss\"""); 

Custom Date and Time Format Strings (MSDN)

+1
source

I would say like this:

 var now = DateTime.Now; var date = now.ToString("ddMMyyyy", CultureInfo.InvariantCulture); var time = now.ToString("HHmmss", CultureInfo.InvariantCulture); var dt = string.Format(CultureInfo.InvariantCulture, "\"{0}\",\"{1}\"", date, time); Console.WriteLine(dt); 
0
source

You can try the following:

 var now = DateTime.Now; var formattedDateTime = $"{now.ToString("ddMMyyyy")},{now.ToString("HHmmss")}"; 
0
source

All Articles