Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate Date and time with " (String.Format)

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

like image 480
MarkL Avatar asked Jul 01 '16 07:07

MarkL


People also ask

How do I separate date and time from string?

String str = "8/29/2011 11:16:12 AM"; String fmt = "MM/dd/yyyy HH:mm:ss a"; DateFormat df = new SimpleDateFormat(fmt); Date dt = df. parse(str); DateFormat tdf = new SimpleDateFormat("HH:mm:ss a"); DateFormat dfmt = new SimpleDateFormat("MM/dd/yyyy"); String timeOnly = tdf. format(dt); String dateOnly = dfmt.

What is string date/time format?

A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.

How do I format a string to a date?

parse(str) can read a date from a string. The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day.

How do you format date and time?

On the Home tab, click the Dialog Box Launcher next to Number. You can also press CTRL+1 to open the Format Cells dialog box. In the Category box, click Date or Time, and then choose the number format that is closest in style to the one you want to create.


2 Answers

You can try formatting:

 DateTime dt = DateTime.Now;
 // "01072016","101511"
 string d = String.Format("\"{0:ddMMyyyy}\",\"{0:HHmmss}\"", dt);  
like image 94
Dmitry Bychenko Avatar answered Oct 04 '22 11:10

Dmitry Bychenko


" 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)

like image 31
Christian Hayter Avatar answered Oct 04 '22 10:10

Christian Hayter