简体   繁体   English

解析/分解DateTime.Now

[英]parsing/breaking up DateTime.Now

I need to break up date time, currently when I use: 我需要在当前使用时分拆日期时间:

Write(DateTime.Now);

it displays: 5/17/2011 03:30:00 PM 它显示:5/17/2011 03:30:00 PM

What I need it to look like is: 5/17/2011, 03:30:00 PM 我需要它看起来像是:5/17/2011,03:30:00 PM

I basically need a comma between the date & time. 我基本上需要在日期和时间之间使用逗号。

My thinking was to parse them and write them separately, but I have been unsuccessful in my attempts. 我的想法是解析它们并分别编写它们,但是我的尝试一直没有成功。

Any thoughts or suggestions on how to do this. 有关如何执行此操作的任何想法或建议。

You can use the .ToString() method which takes in a format. 您可以使用采用格式的.ToString()方法。

DateTime.Now.ToString("MM/dd/yyyy, hh:mm:ss tt");

Here is a nice little reference guide to string formatting 这是有关字符串格式的很好的参考指南

Try looking at the ToShortDateString and ToShortTimeString methods. 尝试查看ToShortDateStringToShortTimeString方法。 You should be able to accomplish what you need in a variety of ways. 您应该能够以各种方式完成所需的工作。

Write(string.Format("{0}, {1}", DateTime.Now.ToShortDateString(), DateTime.Now.ToShortTimeString()));
DateTime dateTime = DateTime.Now;
Write(string.Format("{0}, {1}", dateTime.ToShortDateString(), dateTime.ToLongTimeString()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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