我们如何自动找到激励日期格式 [英] How can we find excat date format automatically

查看:73
本文介绍了我们如何自动找到激励日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:2016-05-05哪一个是日期哪一个是月,在这种情况下我们如何识别完全可以任何人帮助我



我尝试了什么:



示例:2016-05-05哪一个是日期,其中一个是月 case我们如何识别确切可以任何人帮助我

Example : 2016-05-05 which one is "date" which one is "month" in this case how can we identify Exactly can any one help me

What I have tried:

Example : 2016-05-05 which one is "date" which one is "month" in this case how can we identify Exactly can any one help me

推荐答案

如果你不知道哪个是月和日,那么代码应该如何知道呢?在该特定格式中,99%可能是YYYY-MM-dd。但是,您只能确定是否查看生成字符串的代码。请记住,2016-05-05是一个字符串,而不是日期。它曾经是一个日期,直到有人将它转换为字符串,当他们这样做时,他们决定字符串应该采用什么格式。如果你想把那个字符串变回日期你需要知道格式是什么。



当处理日期时,字符串总是坚持一个已知的,最好是不明确的,格式。从技术上讲,YYYY-MM-dd是一种明确的SQL格式,但我个人更喜欢dd MMM YYYY,即2016年8月16日。



SQL Server明确的日期字符串 - Matt Randle [ ^ ]



您选择的格式并不重要,你需要确保它在整个系统中是通用的。
If you don't know which is month and day then how is the code supposed to know? In that specific format it is 99% likely to be YYYY-MM-dd. However you'll only know for sure if you look at the code that generated the string. Remember that "2016-05-05" is a string, not a date. It was once a date until someone converted it to a string, and when they did that they dictated what format the string should take. If you want to turn that string back into the date you need to know what the format was.

When dealing with dates as strings always stick to a known, preferably unambigous, format. Technically YYYY-MM-dd is an unambiguous SQL format, but I personally prefer "dd MMM YYYY" which is "16 Aug 2016".

SQL Server unambiguous date strings - Matt Randle[^]

What format you choose doesn't really matter, you just need to ensure it is universal across your whole system.


嗯...从编程角度来看   date是date  ,仅此而已!日期格式取决于区域设置。请参阅:

全球化循序渐进:日期格式化 [ ^ ]

标准日期和时间格式字符串 [ ^ ]

自定义日期和时间格式字符串 [ ^ ]

格式化特定文化的日期和时间 [ ^ ]



因此,无论日期格式如何,您都应该能够使用 DateTime.TryParse方法(字符串,日期时间)(系统) [ ^ ]:

Well... from programming perspective  date is date  and nothing more! Date format depends on locale settings. See:
Globalization Step-by-Step: Date Formatting[^]
Standard Date and Time Format Strings[^]
Custom Date and Time Format Strings[^]
Formatting Date and Time for a Specific Culture[^]

So, no matter of date format, you should be able to convert string to date by using DateTime.TryParse Method (String, DateTime) (System)[^]:
string customDate = "2016-05-05";
DateTime outDate = new DateTime(1901,1,1);

if (DateTime.TryParse(customDate, out outDate))
{
	Console.WriteLine("Result date: '{0}'", outDate);
}
else
{
	Console.WriteLine("Conversion from '{0}' to date failed!", outDate);
}


这篇关于我们如何自动找到激励日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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