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);
Related
I want to convert String into DateTime. Everything is correct but don't know why I am getting this error;
String was not recognized as a valid DateTime
string dtf = hdnFromDate.Value;
(While debugging I can see dtf value is Sun Dec 13 2020 00:00:00 GMT+0300 (Arab Standard Time)) and I am trying to convert into DateTime But no success
I am converting in this way
DateTime date = DateTime.ParseExact(dtf, "dd/MM/yyyy", null);
I also try like this
DateTime dt= DateTime.ParseExact(dtf,
"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(Arab Standard Time)'",
CultureInfo.InvariantCulture);
Where I am doing wrong?
Reference to DateTime.ParseExact Method
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
So your datetime string should like this otherwise you will get exception
Mon Dec 14 2020 14:42:46 GMT+08:00 (Arab Standard Time)
or
Mon Dec 14 2020 14:42:46 GMT+0800 (Arab Standard Time)
You can try this to get what kind of string you need
Console.WriteLine(
DateTime.Now.ToString("ddd MMM dd yyyy HH: mm:ss 'GMT'K '(Arab Standard Time)'",
CultureInfo.InvariantCulture)
);
If your string have whitespace li
Mon Dec 14 2020 14: 42:46 GMT+08:00 (Arab Standard Time)
Yoo can try
Console.WriteLine(
DateTime.Now.ToString("ddd MMM dd yyyy HH: mm:ss 'GMT'K '(Arab Standard Time)'",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces)
);
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);
Im making a post from a view and getting it in a actionresult as a string.
The value I get is:
Tue Feb 18 2014 00:00:00 GMT-0300 (Hora oficial do Brasil)
Using DateTime.Parse throws an exception:
String was not recognized as a valid DateTime.
What makes this string invalid, and how can I successfully convert it to a DateTime?
DateTime.Parse throws exception for this string because it does not have a standart date/time format.
If your GMT-0300 (Hora oficial do Brasil) is stable in your string, you can use;
var s = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora oficial do Brasil)";
var date = DateTime.ParseExact(s,
"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(Hora oficial do Brasil)'",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
2/18/2014 12:00:00 AM
Here is a demonstration.
I don't think there is a way to parse your (Hora oficial do Brasil) part except using string delimiter.
Take a look at;
The "K" Custom Format Specifier
I don't know why K specifier doesn't work on Ideone actually. I have to put -0300 part also as a string delimiter for generating example. It can be an issue with DateTimeKind enumeration but I'm not sure..
The string is invalid because of the 'GMT' and the '(Hora oficial do Brasil)' parts.
Simply put: the parser is unable to determine what is part of a date time and what is not.
By using format strings you will be able to parse the string into the DateTime format.
see: MSDN: Custom Date and Time Format Strings
in your case this format string will work: "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora oficial do Brasil)'".
You can use it like this:
string input = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora official do Brasil)";
string[] format = { "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora official do Brasil)'" };
DateTime date;
if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
//Yepee the input was parsed correct
}
else
{
//system was unable to parse the string
}
Or like this if error handling is not nessesary:
string input = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora official do Brasil)";
string format = "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora official do Brasil)'";
DateTime date = DateTime.ParseExact(input, format, 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);
This is my way to convert but it's not work:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert =
DateTime.ParseExact(date,
"dd/MM/yyyy HH:mm:ss",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateConvert);
so, how to convert it? Thankyou!
Your format string for the DateTime.ParseExact should be ddd MMM dd HH:mm:ss 'ICT' yyyy
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx on the details on DateTime format strings.
PARSE EXACT means you're telling it to expect:
dd/MM/yyyy HH:mm:ss
But your input string is not in that format!!!
You need something like:
ddd MMM dd HH:mm:ss \I\C\T yyyy
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime d = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss ICT yyyy", null);
Console.WriteLine(d.ToString("dd/MM/yyyy HH:mm:ss"));
What you've got wrong is that the format you provide to DateTime.ParseExact is supposed to be the format of the date string coming in, not what you want coming out.
Try something like:
string date = "Mon Nov 12 08:00:00 ICT 2012";
DateTime dateConvert = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss 'ICT' yyyy", null);
Console.WriteLine(dateConvert.ToString("dd/MM/yyyy HH:mm:ss"));
Thanks for all care! This is my solution and it's work cool!
DateTime dateTime = DateTime.ParseExact("Mon Nov 12 08:00:00 ICT 2012", "ddd MMM dd HH:mm:ss ICT yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dateTime.ToString("MM/dd/yyyy"));