Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime to string, formatting a line break into string while preserving culture

I have a timestamp that I would like to represent as two lines. Currently, I am using "timeStamp.ToString("dd/MM/yyyy \n hh:mm:ss tt")."

I would like to preserve the culture for this so that, when globalization is a concern, this time stamp is displayed correctly.

Is there a simple way to achieve this? Or, do I need to do something like... string.Format("{0}\n{1}", timeStamp.Date.ToString(), timeStamp.Time.ToString() ); ?

Thanks

like image 814
Sean Anderson Avatar asked Nov 24 '25 12:11

Sean Anderson


2 Answers

You can use Date and Time Format Strings to format a DateTime value in a custom way:

string result = string.Format("{0:d}\n{0:T}", timestamp);

// result == "6/15/2009\n1:45:30 PM" (en-US)
// result == "15.06.2009\n13:45:30"  (de-DE)
like image 158
dtb Avatar answered Nov 26 '25 03:11

dtb


You seem to want to preserve the culture information, the easiest way to do this is to use your own example.

string.Format("{0}\r\n{1}", timeStamp.ToShortDateString(), timeStamp.ToLongTimeString());
like image 32
Bas Avatar answered Nov 26 '25 02:11

Bas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!