I am getting error while converting string to datetime.
string dateStarted= "Wed Nov 27 2019 20:37:46 GMT+0700 (Indochina Time)";
DateTime startedDate = DateTime.Parse(dateStarted);
You should use ParseExact to convert string to datetime
var date = DateTime.ParseExact(
"Wed Nov 27 2019 20:37:46 GMT+0700 (Indochina Time)",
"ddd MMM dd yyyy HH:mm:ss 'GMT+0700 (Indochina Time)'",
CultureInfo.InvariantCulture);
How can I convert DateTime to the following format:
Sat Feb 17 2018 13:35:33 GMT+0200 (EET)
Try following :
DateTime date = DateTime.Now;
DateTime EET = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("E. Europe Standard Time"));
string dateEE = EET.ToString(#"ddd MMM dd yyyy HH:mm:ss G\MT+0200");
How to convert a string date value of such format:
Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)
To a date of format of 02/10/2013.
I have already tried the DateTime.ParseExact but it didn't work at all:
DateTime.ParseExact(dateToConvert, "ddd MMM d yyyy HH:mm:ss GMTzzzzz", CultureInfo.InvariantCulture);
var dateToConvert = "Wed Oct 02 2013 00:00:00 GMT+0100 (GMT Daylight Time)";
var format = "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(GMT Daylight Time)'";
var date = DateTime.ParseExact(dateToConvert, format, CultureInfo.InvariantCulture);
Console.WriteLine(date); //prints 10/2/2013 2:00:00 AM for my locale
You need to specify ending with 'GMT'zzz '(GMT Daylight Time)'
You can use single d in format instead of dd, works fine
You can check demo here
Thank you. This is exactly what I have done to resolve the question in the last comment above.
int gmtIndex = dateToConvert.IndexOf("G");
string newDate = dateToConvert.Substring(0, gmtIndex).Trim();
value = DateTime.ParseExact(newDate, "ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Here is the date time format i'm trying to format.I'm getting this date format from twitter apis
string date = "Thu Jul 18 17:39:53 +0000 2013"
i tried
Convert.ToDateTime(date).ToString("dd/MM/yyyy")
But it says String was not recognized as a valid DateTime.
This works:
DateTime.ParseExact(dtStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture)
ParseExact and TryParseExact allows to use a custom format string. ddd is the abbreviated day name, MMM the abbreviated month name, dd the day number, HH hours in 24h clock format, mm minutes, ss seconds, zzzz the time-zone and yyyy the years.
I have used CultureInfo.InvariantCulture to specify that the current culture is not used but InvariantCulture which is similar to "en-US".
Demo
works but after getting date from your line of code i tried to do
date.ToString("dd/mm/yyyy") but get the string as 12-12-2013, no
slashes
/ is a replacement character for your current culture's date-separator which is obviously -. So also use CultureInfo.InvariantCulture to specify that the separator should be used without using your current culture:
string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
See: The "/" Custom Format Specifier
Try this
DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)
Its better to use Invariant culture than Current culture
You are trying to convert a non-standard format, so use this:
string dateStr = "Thu Jul 18 17:39:53 +0000 2013";
DateTime date = DateTime.ParseExact(dateStr, "ddd MMM dd h:mm:ss KKKK yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or build the correct format for your input.
How about like;
string date = "Thu Jul 18 17:39:53 +0000 2013";
DateTime dt = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
18.07.2013 20:39:53
K for time zone information in here.
Check out for more information;
Custom Date and Time Format Strings
Your date string needs to be this:
Thu Jul 18 2013 17:39:53 +0000
Whatever is producing your string needs to have the year value after the month and day and before the time, like above.
string date = "Thu Jul 18 2013 17:39:53 +0000";
var theDate = Convert.ToDateTime(date);
Note: This will produce a valid .NET DateTime object.
UPDATE:
If you cannot change the string produced, then use the ParseExact method with a custom format, like this:
string date = "Thu Jul 18 17:39:53 +0000 2013";
var theDate = DateTime.ParseExact(date, "ddd MMM dd H:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Try using DateTime.ParseExact.
string date = "Thu Jul 18 17:39:53 +0000 2013"
DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
I have the following string which I want to bind to DateTime object for further processing:
Fri Dec 7 16:36:21 2012
I tried this:
string format = "ddd MMM dd hh:mm:ss yyyy";
DateTime.ParseExact(_srdfLag.CaptureTime, format,
CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);
However, it throws an exception: String was not recognized as a valid DateTime
What is wrong with my code?
You have 24 hour date change format accordingly, you need HH instead of hh, also used instead of dd.
string date = "Fri Dec 7 16:36:21 2012";
string format = "ddd MMM d HH:mm:ss yyyy";
DateTime dt = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces);