Convert.ToDateTime:如何设置格式 [英] Convert.ToDateTime: how to set format
本文介绍了Convert.ToDateTime:如何设置格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下转换:
Convert.ToDateTime(value)
但是我需要将日期转换为 mm / yy之类的格式。
我正在寻找这样的东西:
but i need convert date to format like "mm/yy".
I'm looking for something like this:
var format = "mm/yy";
Convert.ToDateTime(value, format)
推荐答案
您可能应该使用 DateTime.ParseExact
a>或 DateTime.TryParseExact
。它们允许您指定特定格式。我个人更喜欢 Try
版本,因为我认为它们为错误情况提供了更好的代码。
You should probably use either DateTime.ParseExact
or DateTime.TryParseExact
instead. They allow you to specify specific formats. I personally prefer the Try
-versions since I think they produce nicer code for the error cases.
这篇关于Convert.ToDateTime:如何设置格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文