英文:
DateTime issue when hour is >11
问题
我正在开发一个自定义日期格式转换器,一切都运行正常,直到小时大于11时。目前我们正在使用24小时制,但当我切换到12小时制时,使用长时间转换器时出现相同的行为。当小时达到中午时,它会格式化为12小时之前。
以下是代码部分的翻译:
public static string DateFormatConverter(string date)
{
DateTime result = DateTime.ParseExact(date, "yyyyMMddhhmmss", null);
string month = result.ToLongDateString().Replace(DateTime.Now.DayOfWeek + ",", "");
month = month.Replace(DateTime.Now.Year.ToString(), "");
month = month.Replace(DateTime.Now.Day.ToString() + ",", "");
date = result.Day.ToString() + " " + month + result.ToString("yy") + " " + result.ToString("HH:mm:ss");
date = date.Replace(" ", " ");
return date;
}
这是输入部分的翻译:
- 20230629105843
- 20230629124942
这是输出部分的翻译:
- 29 6月 23 10:58:43
- 29 6月 23 00:49:42
英文:
I'm working on a custom date format converter and everything seems to work fine until I get an hour > 11. Currently we're using 24-hour format but I get the same behavior when I revert to 12 hr format using the long time converter. As as the hour reaches noon it formats 12 hrs behind.
public static string DateFormatConverter(string date)
{
DateTime result = DateTime.ParseExact(date, "yyyyMMddhhmmss", null);
string month = result.ToLongDateString().Replace(DateTime.Now.DayOfWeek + ",", "");
month = month.Replace(DateTime.Now.Year.ToString(), "");
month = month.Replace(DateTime.Now.Day.ToString() + ",", "");
date = result.Day.ToString() + " " + month + result.ToString("yy") + " " + result.ToString("HH:mm:ss");
date = date.Replace(" ", " ");
return date;
}
Here is the input:
> 1)20230629105843
> 2)20230629124942
Here is the output:
> 1)29 June 23 10:58:43
> 2)29 June 23 00:49:42
答案1
得分: 4
"See the docs, hh
is for 12 hour format, you need HH
for 24 hour format."
英文:
See the docs, hh
is for 12 hour format, you need HH
for 24 hour format
答案2
得分: 0
以下是您要翻译的内容:
"对于那些可能面临与.NET类似问题的人,我已发布了一个简化版本的函数,并进行了更正,因为有一些我认为可能会让一些人感到困惑的不必要的代码行。"
public static string DateFormatConverter(string date)
{
DateTime result = DateTime.ParseExact(date, "yyyyMMddHHmmss", null);
date = result.Day.ToString() + " " + result.ToString("MMMM") + " " + result.ToString("yy") + " " + result.ToString("HH:mm:ss");
date = date.Replace(" ", " ");
return date;
}
英文:
So for those of you who might be facing a similar issue with .net I've posted a version of a simplified version of the function with the correction, since there was some unnecessary lines of code that I thought might confuse some people.
public static string DateFormatConverter(string date)
{
DateTime result = DateTime.ParseExact(date, "yyyyMMddHHmmss", null);
date = result.Day.ToString() + " " + result.ToString("MMMM")+" " + result.ToString("yy") + " " + result.ToString("HH:mm:ss");
date = date.Replace(" ", " ");
return date;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论