日期时间转换为yyyy / M / d反正转换为M / d / yyyy [英] Datetime convert to yyyy/M/d anyway converts to M/d/yyyy

查看:223
本文介绍了日期时间转换为yyyy / M / d反正转换为M / d / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式为11/19/2017 12:00:40 AM,我想将其转换为2017/11/19 12:00:40 AM

I have date on format 11/19/2017 12:00:40 AM and I want to transform it to 2017/11/19 12:00:40 AM

var d = DateTime.ParseExact("11/19/2017 12:00:40 AM", "M/d/yyyy hh:mm:ss tt", 
    CultureInfo.InvariantCulture).ToString("yyyy/M/d hh:mm:ss tt");

var dt = DateTime.ParseExact(d, "yyyy/M/d hh:mm:ss tt", CultureInfo.InvariantCulture);

d的值实际上是2017/11/19 12:00:40 AM,但它是字符串。在db中,我必须将其写为 yyyy / M / d hh:mm:ss tt

value of d is actually 2017/11/19 12:00:40 AM but it's string. In db I must write it as yyyy/M/d hh:mm:ss tt.

但是结果为dt仍然是旧值。

But result of dt is anyway the old value.

出了什么问题?

推荐答案

您正在遇到值与实际值的表示形式。

You are running into representation of a value vs the actuall value.

实际上所有dateTimes都是无符号的Int64,计数自点X以来的Ticks数量(通常是Unix时间的开始,1970年1月1日,00:00:00)。但是用户不能使用它。他们需要人类可读的字符串格式。朗格日(Langauges)之间的威奇(Wich)也有所不同,文化地区则少得多。英国和美国都说英语,但是无法决定是月还是日。

In reality all dateTimes are a unsigned Int64 counting the number of Ticks since "point X" (wich is usually the start of the Unix Time, 1st Janurary 1970, 00:00:00). But users can not work with that. They need a human readable string format. Wich is also different between Langauges, much less Cultural regions. UK and US both speak english, but can not decide if the month or day should come first.

ToString()和Parse()函数均旨在自动提取在Windows的区域设置中,数字应显示为字符串的正确方式。调试器中的ToString()函数也不例外。

Both the ToString() and Parse() functions are designed to automagically extract the proper way a Number should look as string from the Region settings of Windows. The ToString() function that is part of the Debugger is no exception.

这篇关于日期时间转换为yyyy / M / d反正转换为M / d / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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