在C#中的DateTime字符串格式 [英] DateTime string format in c#

查看:185
本文介绍了在C#中的DateTime字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个字符串变量的一个项目。

I have a project that contain 3 string variables.


  1. DateFormatStr 是格式字符串我需要使用输出日期。

  2. DateFormatFrom 是的请求将从
  3. $申请起始日期b $ b
  4. FilloutDateTo 为结束日期的要求将适用于。

  1. DateFormatStr is the format string I need to use to output dates.
  2. DateFormatFrom is the start date a request will apply from
  3. FilloutDateTo is the end date the request will apply to.

问题是,我不想手动指定的日期。正如你可以在下面我举的例子看(工作示例),我需要指定的日期,但有没有办法让它的起始日期有时间00:00:00,结束日期有时间23时59分: ?59

The problem is that I don't want to manually specify the dates. As you can see in my example below (a working example), I need to specify the dates, but is there a way to make it that the from date has time 00:00:00 and the end date has time 23:59:59?

string DateFormatStr = "MM/dd/yy hh:mm:ss tt";
string DateFormatFrom = "12/04/14 00:00:00";
string FilloutDateTo = "12/04/14 23:59:59";



所以,我想系统时间从日期和开始日期承认尊重 formatStr 变量。

感谢

推荐答案

如果我理解正确的话,你可以使用的 DateTime.Today 属性喜欢;

If I understand correctly, you can use DateTime.Today property like;

var dt1 = DateTime.Today;
var dt2 = DateTime.Today.AddDays(1).AddSeconds(-1);

和使用的 则DateTime.ToString() 进行格式化喜欢;

and use DateTime.ToString() to format them like;

var DateFormatFrom = dt1.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var FilloutDateTo = dt2.ToString("MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);



结果会;

Results will be;

12/04/2014 00:00:00
12/04/2014 23:59:59

您使用的 HH 格式说明但它是12小时制。使用 HH 格式说明,而不是它是24小时制。而且,由于你的结果字符串没有任何AM / PM指示,你并不需要使用 TT 格式说明。

You used hh format specifier but it is for 12-hour clock. Use HH format specifier instead which is for 24-hour clock. And since your result strings doesn't have any AM/PM designator, you don't need to use tt format specifier.

这篇关于在C#中的DateTime字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆