Separate Date and time with " (String.Format)
Asked Answered
S

4

5

Is it possible to separate Date and time with ".

So it would be:

"ddMMyyyy","HHmmss"

Right now i have:

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

and String.Format shows me just "ddMMyyyy,HHmmss"

Thank you everyone for helping me !!! But i will mark the first answer as the right one

Senzer answered 1/7, 2016 at 7:8 Comment(2)
What you ware expectedCoster
@un-lucky i know that " \"ddMMyyyy\",\"HHmmss\" " and that [ \" ] does nothing , this is why i ask here.Senzer
B
6

You can try formatting:

 DateTime dt = DateTime.Now;
 // "01072016","101511"
 string d = String.Format("\"{0:ddMMyyyy}\",\"{0:HHmmss}\"", dt);  
Balf answered 1/7, 2016 at 7:15 Comment(0)
L
1

" is a formatting character, so it needs to be escaped with \, e.g.

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

You may find a verbatim string slightly more readable:

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

Custom Date and Time Format Strings (MSDN)

Lanceted answered 1/7, 2016 at 7:15 Comment(0)
S
0

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);
Sagacious answered 1/7, 2016 at 7:15 Comment(1)
Probably would have been wicked if you stored the result of padding it with " and , into a variable rather than doing it time via Console.WriteLineAimee
S
0

You can try this:

var now = DateTime.Now;
var formattedDateTime = $"{now.ToString("ddMMyyyy")},{now.ToString("HHmmss")}";
Silvana answered 1/7, 2016 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.