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
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.
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.
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.
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.
You can try formatting:
DateTime dt = DateTime.Now;
// "01072016","101511"
string d = String.Format("\"{0:ddMMyyyy}\",\"{0:HHmmss}\"", dt);
"
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With