简体   繁体   中英

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

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)

您似乎想要保留文化信息,最简单的方法是使用您自己的示例。

string.Format("{0}\r\n{1}", timeStamp.ToShortDateString(), timeStamp.ToLongTimeString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM