开发者

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

开发者 https://www.devze.com 2023-02-19 07:13 出处:网络
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 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() ); ?

Tha开发者_如何学Cnks


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)


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());
0

精彩评论

暂无评论...
验证码 换一张
取 消