C# 日期时间转换为“YYYYMMDDHHMMSS”格式
我想将 C# DateTime 转换为“YYYYMMDDHHMMSS”格式。但我没有找到获取这种格式的内置方法?有什么意见吗?
I want to convert a C# DateTime to "YYYYMMDDHHMMSS" format. But I don't find a built in method to get this format? Any comments?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
这个网站有很棒的示例看看
标准日期时间格式
更新
使用 C# 6 字符串插值格式
This site has great examples check it out
Standard DateTime Formatting
Update
using c# 6 string interpolation format
您实际上已经自己编写了格式。
yourdate.ToString("yyyyMMddHHmmss")
其他一切都应该是不言自明的。
You've practically written the format yourself.
yourdate.ToString("yyyyMMddHHmmss")
Everything else should be self-explanatory.
您只需在月份 (MM) 和分钟 (mm) 之间小心:(
另请注意,HH 是 24 小时制,而 hh 是 12 小时制,通常与 am/pm 指示符的 t 或 tt 结合使用.)
如果您想将其作为复合格式字符串的一部分来执行,您可以使用:
有关详细信息,请参阅 有关自定义日期和时间格式的 MSDN 页面。
You've just got to be careful between months (MM) and minutes (mm):
(Also note that HH is 24 hour clock, whereas hh would be 12 hour clock, usually in conjunction with t or tt for the am/pm designator.)
If you want to do this as part of a composite format string, you'd use:
For further information, see the MSDN page on custom date and time formats.
您可以使用自定义格式字符串:
如果您不需要 24 小时时钟时间,请将“HH”替换为“hh”。
You can use a custom format string:
Substitute "hh" for "HH" if you do not want 24-hour clock time.
如果您使用 ReSharper,请获取有关“:”的帮助(参见图片)
If you use ReSharper, get help with ':' (see image)
在
.Net Standard 2
中,您可以格式化DateTime
,如下所示:In
.Net Standard 2
you can formatDateTime
like belows:如果你只想将其显示为字符串
if you just want it displayed as a string
我很惊讶没有人有这方面的链接。
可以使用此处的指南创建任何格式:
自定义日期和时间格式字符串
对于您的具体示例(正如其他人指出的那样),请使用类似的内容
,其中 my_format 可以是 y、M、H、m、s、f、F 等的任何字符串组合!
查看链接。
I am surprised no one has a link for this .
any format can be created using the guidelines here:
Custom Date and Time Format Strings
For your specific example (As others have indicated) use something like
Where my_format can be any string combination of y,M,H,m,s,f,F and more!
Check out the link.
获取日期作为
DateTime
对象而不是字符串。然后您可以根据需要对其进行格式化。点击此处了解更多模式
Get the date as a
DateTime
object instead of a String. Then you can format it as you want.Click here for more patterns
使用 C# 6.0
using C# 6.0
将格式化的日期时间指定为 Utc:
步骤 1 - 初始日期
步骤 2 - 将日期格式化为愿意的格式 (“yyyyMMddHHmmss”)
步骤 3 - 指定日期类型 (Utc)
Specify formatted DateTime as Utc:
Step 1 - Initial date
Step 2 - Format date as willing ("yyyyMMddHHmmss")
Step 3 - Specify kind of date (Utc)
一个简单的方法,完全控制'from type'和'to type',并且只需要记住这个代码以供将来的铸造
An easy Method, Full control over 'from type' and 'to type', and only need to remember this code for future castings
这没什么大不了的。你可以简单地这样说,
因为我使用了 $ ,它用于字符串插值。
It is not a big deal. you can simply put like this
Excuse here for I used $ which is for string Interpolation .
上述任何答案都无法解决您的问题的可能性很小。尽管如此,我正在分享我的方法,该方法始终适用于不同格式的日期时间。
Chances are slim that any of the above answers wouldn't has solved your problem. Nonetheless, I'm sharing my method which always works for me for different format of datetimes.
在 Google 搜索上花了很多时间后,我找到了以下解决方案,当我在本地给出日期时间时,也不例外,而从其他服务器上,则出现错误.........日期格式不正确..在 C# 中保存/搜索文本框日期时间之前,只需检查外部服务器文化是否与数据库服务器文化相同。例如,两者都应该是“en-US”,或者必须都是“en-GB”asp 下面的快照。
即使使用不同的日期格式,如(dd/mm/yyyy)或(yyyy/mm/dd),它也会准确保存或搜索。
After spent a lot of hours on Google search, I found the below solution as when I locally give date time, no exception while from other server, there was Error......... Date is not in proper format.. Before saving/ searching Text box date time in C#, just checking either the outer Serer Culture is same like database server culture.. Ex both should be "en-US" or must be both "en-GB" asp below snap shot.
Even with different date format like (dd/mm/yyyy) or (yyyy/mm/dd), it will save or search accurately.