Parse DateTime From Odd Format [duplicate] - c#

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Parsing a complicated string as DateTime
I have a string date with time that I'd like to parse into a DateTime. I'm not quite sure how to tackle it because of the odd format. Help is appreciated.
Example: Mon Mar 24 13-42-30 2008

DateTime.ParseExact should do what you want:
var dateTime = DateTime.ParseExact(
"Mon Mar 24 13-42-30 2008",
"ddd MMM dd HH-mm-ss yyyy",
CultureInfo.CurrentCulture);
Can someone tell me the difference between using CultureInfo.CurrentCulture and CultureInfo.InvariantCulture, like the other answers? I was assuming things like the day name and month name might need to be parsed in their native language. Thanks.

Try:
var theDate = DateTime.ParseExact(
"Fri Jul 13 13-42-30 2012",
"ddd MMM dd HH-mm-ss yyyy",
System.Globalization.CultureInfo.InvariantCulture);
Date string formatting options can be found here.

That format doesn't seem all that odd, but you could probably easily handle it by using DateTime.ParseExact(). Of course, it assumes that the format remains the same.

try this way please
string format ="ddd MMM dd hh-mm-ss yyyy";
DateTime dt = DateTime.ParseExact(format, dateString, CultureInfo.InvariantCulture);

Related

How to convert datetime format in c#?

I want to convert the DateTime format using c#. This is my code
string date = "Thu May 20 2021 00:00:00 GMT-0700 (Pacific Daylight Time)";
var s = DateTime.ParseExact(date, "dd-MM-yyyy HH:mm:ss", null);
But this code not working the exception is 'System.FormatException: 'String was not recognized as a valid DateTime.'
The format you provide must match with the format used in the string. Hence the name ParseExact. After playing around a bit I was able to match these:
string date = "Thu May 20 2021 00:00:00 GMT-0700"
var s = DateTime.ParseExact(date, "ddd MMM dd yyyy HH:mm:ss \"GMT\"zzz", null);
You may need to manually truncate the (Pacific Daylight Time) or include it in the format as a literal (like I did with GMT here).
For more information you can work with DateTime format specifiers
Your format string means that the date value must be something like that:
string date = "20-05-2021 12:00:00";
var s = DateTime.ParseExact(date, "dd-MM-yyyy HH:mm:ss", null);
Try to use an other date value or change the date format string.
You can find a lot of good examples here:
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=net-5.0#System_DateTime_ParseExact_System_String_System_String_System_IFormatProvider_

How to Convert a datetime to Long Date?

Below is my code,
DateTime DateFrom_ = Convert.ToDateTime(CSO.dateFrom);
I am getting the value currently like - 29/10/2019 00:00:00
what i want is I want to convert above to this format - Tue, 29 Oct 2019
can anyone please guide me how to do this, Thank you.
Below one worked for me,
DateTime DateFrom_ = Convert.ToDateTime(CSO.dateFrom);
var convertedDate = DateFrom_.ToString("ddd', 'dd' 'MMM' 'yyyy");
You can do it easily, using DateTime.ParseExact to parse from certain format, and then use ToString with different format to represent it based on your need:
var date = DateTime.ParseExact("29/10/2019 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
date.ToString("ddd, dd MMM yyyy"); // output Tue, 29 Oct 2019
Please note, if you need culture specific long date representation, then you should use ToString("D", cultureInfo) overload. That way, your format will be aligned with the culture defined format.
I think using DateTime.ToString() with the specific format should do the work. Try this one:
date.ToString("D", CultureInfo.CreateSpecificCulture("en-US"));
As it's been mentioned in here, you want to be using "DateTime.ParseExact"
var str = "Tue, 29 Oct 2019";
var date = DateTime.ParseExact(str, "ddd, dd MMM yyyy", CultureInfo.InvariantCulture);
You may split the date and use DateTime.ParseExact.
string[] tokens = "29/10/2019 00:00:00".Split(' ');
DateTime DateFrom_ = DateTime.ParseExact(tokens[0], "dd/MM/yyyy", CultureInfo.InvariantCulture);
var dateString = DateFrom_.ToString("ddd, dd MMM yyyy");

Why is this DateTime.ParseExact call failing? [duplicate]

This question already has answers here:
Parse DateTime with time zone of form PST/CEST/UTC/etc
(6 answers)
Closed 6 years ago.
var dateValue = "Mon, 02 May 2016 12:00 PM EDT";
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt K",
System.Globalization.CultureInfo.InvariantCulture);
As near as I can tell, from the official format string documentation, this should work. Instead, it raises System.FormatException with the rather unhelpful message: String was not recognized as a valid DateTime.
Is there any way to figure out what's going wrong?
The K Custom Format Specifier does not accept time zone strings.
If you can supply the hour offset instead of a string, then you can use "z".
var dateValue = "Mon, 02 May 2016 12:00 PM -4";
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt z",
System.Globalization.CultureInfo.InvariantCulture);
https://msdn.microsoft.com/en-us/library/shx7s921%28v=vs.110%29.aspx specifies that the DateTime.Kind enumeration has 3 members. So perhaps it does not like you to specify "EDT" as a kind.
Member name Description
Local The time represented is local time.
Unspecified The time represented is not specified as either local time or Coordinated Universal Time (UTC).
Utc The time represented is UTC.
This should work
var dateValue = "Mon, 02 May 2016 12:00 PM EDT".Replace("EDT", "-4");
var date = DateTime.ParseExact(
dateValue,
"ddd, dd MMM yyyy hh:mm tt z",
System.Globalization.CultureInfo.InvariantCulture);

Can this date be converted automatically?

I know that C# has some great date conversion tools. What I'm I'm wondering is if I can automatically convert this string to a date object:
"Fri May 11 00:00:00 EDT 2012"
I'm thinking I'll have to manually parse the month, day, and year but I'm hoping there is an easier way built-in. Any help would be appreciated.
Thanks!
You can use DateTime.ParseExact or DateTime.TryPraseExact to provide a custom format:
DateTime result;
if (!DateTime.TryParseExact(
"Fri May 11 00:00:00 EDT 2012",
"ddd MMM dd HH:mm:ss EDT yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out result)) {
// handle invalid date
}
All of the format options are listed on the Custom Date and Time Format Strings page on MSDN.
Convert.ToDateTime("Fri May 11 00:00:00 EDT 2012") should work just fine.
http://msdn.microsoft.com/en-us/library/xhz1w05e.aspx
Take a look at that. Should help you out.
Example:
// Convert a string returned by DateTime.ToString("R").
String dateString = "Sat, 10 May 2008 14:32:17 GMT";
ConvertToDateTime(dateString);
Yes, you can parse the string into a DateTime object :
String format = "ddd MMM dd hh:mm:ss EDT yyyy";
String dateString = "Fri May 11 00:00:00 EDT 2012";
DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);

How to parse DateTime in this format? "Sun May 23 22:00:00 UTC+0300 2010"

This is a quick one, i wanna parse a date that comes in this format "Sun May 23 22:00:00 UTC+0300 2010"
Is this a valid UTC DateTime? And how to parse it? I tried :
DateTime newStartTime = DateTime.ParseExact(hdnNewStartTime.Value, "ddd MM dd HH:mm:ss UTC+0300 yyyy", CultureInfo.CurrentCulture);
However, this didn't work, any help appreciated!
DateTime dt = DateTime.ParseExact(s,"ddd MMM dd HH:mm:ss UTCzzzz yyyy", System.Globalization.CultureInfo.InvariantCulture);
Its not a standard format, but you can still parse it.
string format = "ddd mmm dd HH:mm:ss zzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
This isn't in a standard .NET format, so you'll probably have to parse it by hand. The UTC+0300 bit indicates the timezone, everything else is part of the date and time.
I tried the solution presented by #johncatfish and it does what I expect. I would presume that you actually want to keep the timezone information.
[Test()]
public void TestCaseWorks ()
{
string format = "ddd MMM dd HH:mm:ss UTCzzzzz yyyy";
string temp = "Sun May 23 22:00:00 UTC+0300 2010";
DateTime time = DateTime.ParseExact(temp, format, CultureInfo.InvariantCulture);
Assert.AreEqual(DayOfWeek.Sunday, time.DayOfWeek);
Assert.AreEqual(5, time.Month);
Assert.AreEqual(23, time.Day);
Assert.AreEqual(0, time.Minute);
Assert.AreEqual(0, time.Second);
Assert.AreEqual(2010, time.Year);
// Below is the only actually useful assert -- making sure the
// timezone was parsed correctly.
// In my case, I am GMT-0700, the target time is GMT+0300 so
// 22 + (-7 - +3) = 12 is the expected answer. It is an exercise
// for the reader to make a robust test that will work in any
// timezone ;).
Assert.AreEqual(12, time.Hour);
}
Sorry for my previous answer which was quite simplistic.
Replace MM by MMM in your date format and it should be fine.
From the example given, it isn't possible to tell if the month should be in the 3 letter form (Jan, Feb, May etc.) or in the full form (January, February, May etc.).
If it should be in the short form, use:
ddd MMM dd HH:mm:ss UTCzzz yyyy
If it should be in the long form, use:
ddd MMMM dd HH:mm:ss UTCzzz yyyy
Details of the formatting specifiers available can be found at http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Categories