1

I want to convert string datetime to Datetime using C#. I am going to store datetime in sql database

4 Answers4

4

The string in your example has an offset component so you can use DateTimeOffset:

var dateTimeOffset = DateTimeOffset.Parse("2012-08-16T19:20:30.456+08:00", CultureInfo.InvariantCulture);

From the linked docs:

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).

haldo
  • 14,512
  • 5
  • 46
  • 52
  • Same error occur "System.FormatException: 'String '2012-08-16T19:20:30.456 08:00' was not recognized as a valid DateTime.' – janith jayaweera Sep 19 '19 at 09:09
  • That's not the string posted in your question... it's missing the `+` sign. What is the actual string you want to parse? – haldo Sep 19 '19 at 09:09
  • Oh. you are right. this is my url parameter "MessageID=23&Status=RECEIVED&UpdatedOn=2012-08-16T19:20:30.456+08:00" when i catch this parameter in controller it show without + public ActionResult SendDR(int MessageID, string Status, string UpdatedOn) – janith jayaweera Sep 19 '19 at 09:14
2

just

DateTime date= DateTime.Parse(dateString);
2
  • Use DateTime.Parse("2012-08-16T19:20:30.456+08:00")
  • Use can use C# Interactive Windows to test it. enter image description here
Ha Doan
  • 611
  • 6
  • 20
2
//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);

These are few C# methods which can be used as a conversion string to DATETIME, make sure string is a valid string so that it allows you to convert.

ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
  • 1
    Correct. when i pass parameter to controller `&UpdatedOn=2012-08-16T19:20:30.456+08:00` '+' mark missing. It catch as `2012-08-16T19:20:30.456 08:00` like this. – janith jayaweera Sep 19 '19 at 09:45
  • Yes exactly, Conversion of dates is too much complicated in C#, whenever the format is invalid its goes on error, so it's very difficult to handle dates as per my point of view. Ha Ha – ankitkanojia Sep 19 '19 at 09:47