在C#中的DateTime字符串格式

编程入门 行业动态 更新时间:2024-10-26 23:30:06
本文介绍了在C#中的DateTime字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a project that contain 3 string variables.

  • DateFormatStr 是格式字符串我需要使用输出日期。
  • DateFormatFrom 是的请求将从
  • $申请起始日期b $ b
  • FilloutDateTo 为结束日期的要求将适用于。
  • DateFormatStr is the format string I need to use to output dates.
  • DateFormatFrom is the start date a request will apply from
  • 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字符串格式

    本文发布于:2023-11-22 15:21:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1617954.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:字符串   格式   DateTime

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!