在C#中以yyyy-MM-dd HH:mm:ss格式将字符串转换为日期时间 [英] convert string to datetime with form yyyy-MM-dd HH:mm:ss in C#

查看:946
本文介绍了在C#中以yyyy-MM-dd HH:mm:ss格式将字符串转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 2014-01-01 23:00:00 转换为 DateTime

Console.WriteLine(DateTime.ParseExact("2014-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));

,结果是这样的:

1/1/2014 11:00:00 PM

推荐答案

我认为您的解析有效。问题是当转换回字符串时。您可以在参数中提供所需的格式:

I think your parsing worked. The problem is when converting back to string. You can provide the desired format in parameter :

DateTime date = DateTime.ParseExact("2010-01-01 23:00:00", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
string formattedDate = date.ToString("yyyy-MM-dd HH:mm:ss")
Console.WriteLine(formattedDate);

默认情况下(没有指定格式),它使用从当前区域性派生的格式设置信息。

By default (without a specified format), it uses formatting information derived from the current culture.

这篇关于在C#中以yyyy-MM-dd HH:mm:ss格式将字符串转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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