如何使用 C# 将此“2012-08-16T19:20:30.456+08:00”字符串转换为 DateTime
How can I convert this "2012-08-16T19:20:30.456+08:00" string to DateTime using C#
我想使用 C# 将字符串日期时间转换为日期时间。我要将日期时间存储在 sql 数据库中
您的示例中的字符串具有偏移分量,因此您可以使用 DateTimeOffset:
var dateTimeOffset = DateTimeOffset.Parse("2012-08-16T19:20:30.456+08:00", CultureInfo.InvariantCulture);
来自链接文档:
The DateTimeOffset structure includes a DateTime value, together with
an Offset property that defines the difference between the current
DateTimeOffset instance's date and time and Coordinated Universal Time
(UTC).
就
DateTime date= DateTime.Parse(dateString);
- 使用
DateTime.Parse("2012-08-16T19:20:30.456+08:00")
- 使用可以使用C# Interactive Windows进行测试。
//string value of date
var iDate = "2012-08-16T19:20:30.456+08:00";
//Convert.ToDateTime(String)
//This method will converts the specified string representation of a date and time to an equivalent date and time value
var dateConversion1 = Convert.ToDateTime(iDate);
//DateTime.Parse()
//DateTime.Parse method supports many formats. It is very forgiving in terms of syntax and will parse dates in many different formats. That means, this method can parse only strings consisting exactly of a date/time presentation, it cannot look for date/time among text.
var dateConversion2 = DateTime.Parse(iDate);
//DateTime.ParseExact()
//ParseExact method will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. The format of the string representation must match the specified format exactly.
var dateConversion3 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", null);
//CultureInfo
//The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.
var dateConversion4 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", System.Globalization.CultureInfo.InvariantCulture);
//DateTime.TryParse method
//DateTime.TryParse converts the specified string representation of a date and time to its DateTime equivalent using the specified culture - specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.
DateTime dateConversion5;
DateTime.TryParse(iDate, out dateConversion5);
这些是一些C#方法,可以用作DATETIME的转换字符串,确保字符串是一个有效的字符串,以便它允许你转换。
我想使用 C# 将字符串日期时间转换为日期时间。我要将日期时间存储在 sql 数据库中
您的示例中的字符串具有偏移分量,因此您可以使用 DateTimeOffset:
var dateTimeOffset = DateTimeOffset.Parse("2012-08-16T19:20:30.456+08:00", CultureInfo.InvariantCulture);
来自链接文档:
The DateTimeOffset structure includes a DateTime value, together with an Offset property that defines the difference between the current DateTimeOffset instance's date and time and Coordinated Universal Time (UTC).
就
DateTime date= DateTime.Parse(dateString);
- 使用
DateTime.Parse("2012-08-16T19:20:30.456+08:00")
- 使用可以使用C# Interactive Windows进行测试。
//string value of date
var iDate = "2012-08-16T19:20:30.456+08:00";
//Convert.ToDateTime(String)
//This method will converts the specified string representation of a date and time to an equivalent date and time value
var dateConversion1 = Convert.ToDateTime(iDate);
//DateTime.Parse()
//DateTime.Parse method supports many formats. It is very forgiving in terms of syntax and will parse dates in many different formats. That means, this method can parse only strings consisting exactly of a date/time presentation, it cannot look for date/time among text.
var dateConversion2 = DateTime.Parse(iDate);
//DateTime.ParseExact()
//ParseExact method will allow you to specify the exact format of your date string to use for parsing. It is good to use this if your string is always in the same format. The format of the string representation must match the specified format exactly.
var dateConversion3 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", null);
//CultureInfo
//The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.
var dateConversion4 = DateTime.ParseExact(iDate, "yyyy-MM-dd HH:mm tt", System.Globalization.CultureInfo.InvariantCulture);
//DateTime.TryParse method
//DateTime.TryParse converts the specified string representation of a date and time to its DateTime equivalent using the specified culture - specific format information and formatting style, and returns a value that indicates whether the conversion succeeded.
DateTime dateConversion5;
DateTime.TryParse(iDate, out dateConversion5);
这些是一些C#方法,可以用作DATETIME的转换字符串,确保字符串是一个有效的字符串,以便它允许你转换。