Ian*_*ink 5 c# datetime datetime-format
我收到了以下格式的日期:
18/04/2011 4:57:20 PM
Run Code Online (Sandbox Code Playgroud)
该DateTime.Parse()方法无法访问它.
有没有办法让它转换18/04/2011 4:57:20 PM为返回一个Date对象April 18, 2011?
这看起来不像标准格式.日期是en-GB,时间是en-US.因此,我建议您使用DateTime.ParseExact并传递格式:
DateTime parsed = DateTime.ParseExact("18/04/2011 4:57:20 PM",
"dd/MM/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
首先,要更改日期的格式,您需要一个DateTime值.您无法格式化字符串日期.使用DateTime.ParseExact从格式化日期字符串中提取日期值:
DateTime dateValue =
DateTime.ParseExact(stringDateValue, "dd/MM/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用DateTime.ToString(format)来提取格式化的字符串值:
resultStringDateValue = dateValue.ToString("MMM dd, yyyy");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4695 次 |
| 最近记录: |