积极答复者
C#的日期对象,能否格式化为一个字符串,要求是如果日期是当天,就只显示时分秒,不要显示日期部分?

问题
答案
-
Hi,
感谢在MSDN论坛发帖。
二楼的同胞应该可以解决你说的问题,我下面也做了测试,你可以直接拷贝过去测试一下。
DateTime dateTime = DateTime.Now; DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0); Console.WriteLine( (date1 == DateTime.Now) ? dateTime.ToString("hh:mm") : dateTime.ToString("MM-dd hh:mm"));
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 liubin 2017年11月10日 8:41
全部回复
-
好像是要自己实现的:
DateTime.Now.Date == dateTime.Date ? dateTime.ToString("hh:mm") : dateTime.ToString("MM-dd hh:mm")
- 已建议为答案 Leo Y. Cheng 2017年11月10日 4:29
-
Hi,
感谢在MSDN论坛发帖。
二楼的同胞应该可以解决你说的问题,我下面也做了测试,你可以直接拷贝过去测试一下。
DateTime dateTime = DateTime.Now; DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0); Console.WriteLine( (date1 == DateTime.Now) ? dateTime.ToString("hh:mm") : dateTime.ToString("MM-dd hh:mm"));
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 liubin 2017年11月10日 8:41