Convert 1st day string to Datetime - c#

I'm looking for a way to be able to convert a date string like this:
"1st Oct 2018" => 2018-10-01
Or this:
"10th Mar 2015" => 2015-03-10
To it's equivalent string in the format yyyy mm dd. I have tried the following code, but no luck:
DateTime dt = DateTime.ParseExact(stringDate, "yyyy mm dd", CultureInfo.InvariantCulture);

Just in addition to great answer from Daisy:
[TestCase("15th Oct 2018")]
[TestCase("1st Oct 2018")]
[TestCase("2nd Oct 2018")]
[TestCase("3rd Oct 2018")]
[TestCase("3d Oct 2018")]
public void Action(string dateStr)
{
// Act
var dt = DateTime.ParseExact(Regex.Replace(dateStr, "(th|st|nd|rd|d)", ""), "d MMM yyyy", CultureInfo.CurrentCulture);
//Assert
Console.WriteLine(dt);
}
UPD: added great suggestions from Dmitry Bychenko.

A DateTime value doesn't have a format - it's just a value. (Just like an int isn't inherently decimal or hex - you choose how to format it when you convert it to a string, and the default is to use decimal.)
The string you're passing into DateTime.ParseExact is the expected input format - and your strings don't have the format "yyyy mm dd". (Note that in date/time format strings, "mm" means minutes, so you'd want "MM" instead of "mm" anyway... but that won't help here.)
Your date format is nearly "d MMM yyyy" (day, short month name, year) using English month names - but the problem is the ordinal part ("st", "nd", "th"). As far as I know there's no simple way of handling that with DateTime.ParseExact. Instead, I'd probably use a regular expression or simple string replacement to remove the ordinal part, so that you do have a string in the format "d MMM yyyy" and then parse that.
For the string replacement part, the answers to this question are appropriate. So here's a complete example using your sample data:
using System;
using System.Globalization;
class Test
{
static void Main()
{
Console.WriteLine(ParseWithOrdinals("10th Mar 2015"));
Console.WriteLine(ParseWithOrdinals("1st Oct 2018"));
}
private static DateTime ParseWithOrdinals(string input) =>
DateTime.ParseExact(
RemoveOrdinals(input), // Text to parse
"d MMM yyyy", // Format of text
CultureInfo.InvariantCulture); // Expect English month names, Gregorian calendar
// From https://stackoverflow.com/questions/17710561
private static string RemoveOrdinals(string input) =>
input
.Replace("0th", "0")
.Replace("1st", "1")
.Replace("2nd", "2")
.Replace("3rd", "3")
.Replace("11th", "11") // Need to handle these separately...
.Replace("12th", "12")
.Replace("13th", "13")
.Replace("4th", "4")
.Replace("5th", "5")
.Replace("6th", "6")
.Replace("7th", "7")
.Replace("8th", "8")
.Replace("9th", "9");
}
(Note that I haven't formatted the result as yyyy-MM-dd in the output, so you'll get your local date/time format.)

There is also a native approach without string remove / replace / Regex
If you know the characters, you can escape them with the ' character inside the date pattern. So "15th Oct 2018" works for this pattern "d'th' MMM yyyy"
Now there are "st","nd","rd","th" so you can try each of them and chose the working one.
Into the overload of TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) you can pass a range of allowable formats.
string input = "15th Oct 2018";
DateTime result = DateTime.Now;
string[] patterns = { "d'st' MMM yyyy", "d'nd' MMM yyyy", "d'rd' MMM yyyy", "d'th' MMM yyyy"};
bool success = DateTime.TryParseExact(input, patterns, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
if (success)
Console.WriteLine(result.ToString("yyyy-MM-dd"));
https://dotnetfiddle.net/H8ulyo

Related

DateTime Parse ANSI C timestamp

I am trying to parse Date/Time formats in HTTP/1.1 headers as specified in RFC2616
How do I parse an ANSI C timestamp in C#?
The closest I get is:
string dateFormat = "ddd MMM d HH':'mm':'ss yyyy";
DateTime time = DateTime.ParseExact("Mon Jan 2 15:04:05 2006", dateFormat, CultureInfo.InvariantCulture);
The problem lies in "d" which does not accept a leading space in case it is a single digit date. And "dd" requires a leading 0 instead.
Is there any easy way around, or maybe a library that already handles the three allowed date/time formats in HTTP/1.1?
How about using DateTime.TryParseExact overload that takes string[] as a format parameter with AllowInnerWhite style?
string s = "Sun, 06 Nov 1994 08:49:37 GMT";
DateTime dt;
var formats = new[]
{
"ddd, dd MMM yyyy HH:mm:ss 'GMT'",
"dddd, dd-MMM-yy HH:mm:ss 'GMT'",
"ddd MMM d HH:mm:ss yyyy"
};
if(DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture,
DateTimeStyles.AllowInnerWhite, out dt))
{
//
}

String to dateTime in C#?

How Can I convert following date string to dateTime:
Fri, 18 Dec 2009 9:38 am PST
I tried DateTime.Parse(string)
I got following error:
The string was not recognized as a valid DateTime. There is an unknown word starting at index 25. System.SystemException {System.FormatException}
UPDATE
I tried to get weather from yahoo and I tried to get date like this:
Date = DateTime.Parse(feed.Element(yWeatherNS + "condition").Attribute("date").Value),
I debugged it. date attribute is correct (like above).
Thanks.
I don't think there's anything in the BCL which will parse time zone abbreviations. (They should be avoided where possible anyway, as they can be ambiguous.)
If you don't mind losing the time zone information, you can use something like this:
using System;
using System.Globalization;
static class Test
{
static void Main()
{
string text = "Fri, 18 Dec 2009 9:38 am PST";
DateTime parsed = TrimZoneAndParse(text);
Console.WriteLine(parsed);
}
static DateTime TrimZoneAndParse(string text)
{
int lastSpace = text.LastIndexOf(' ');
if (lastSpace != -1)
{
text = text.Substring(0, lastSpace);
}
return DateTime.ParseExact(text,
"ddd, dd MMM yyyy h:mm tt",
CultureInfo.InvariantCulture);
}
}
Note that that assumes a fixed date/time format and culture. Your needs may vary, and you should also consider using TryParse or TryParseExact if this is user input.
If DateTime.Parse can't figure it out automatically, you can use DateTime.ParseExact where you specify the format being used.
In your case this would be something like, you'll need to replace the 'PST' yourself however:
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "Fri, 18 Dec 2009 9:38 am PST";
dateString = dateString.Replace("PST", "-08:00");
string format = "ddd, dd MMM yyyy h:mm tt zzz";
DateTime result = DateTime.ParseExact(dateString, format, provider);
If your program needs to work with different timezone abbrevations, you'll have to build a Dictionary with abbrevation to time zone offset conversions.

C# How to convert String into Time and Date format?

I have a short program which converts a string to both date and time format from a simple string.
However it seems that the String is not recorgnizable for the system to be converted into date time format due to the sequence of the string. The String that should be converted is an example like : "Thu Dec 9 05:12:42 2010"
The method of Convert.ToDateTime have been used but does not work.
May someone please advise on the codes? Thanks!
String re = "Thu Dec 9 05:12:42 2010";
DateTime time = Convert.ToDateTime(re);
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Take a look at DateTime.TryParseExact
DateTime time;
if (DateTime.TryParseExact(re,
"ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture,
DateTimeStyles.None, out time)) {
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
} else {
Console.WriteLine("'{0}' is not in an acceptable format.", re);
}
It is often necessary to give it a hint about the specific pattern that you expect:
Edit: the double-space is a pain, as d doesn't handle that;
DateTime time = DateTime.ParseExact(re.Replace(" "," "),
"ddd MMM d hh:mm:ss yyyy", CultureInfo.CurrentCulture);
Take a look at DateTime.Parse
Try this
DateTime time = Convert.ToDateTime("2010, 9, 12, 05, 12, 42");
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
Not sure if the string input should have the double space but you could pull that out and use geoff's answer.
re = Regex.Replace(re, #"\s+", " ");
Other option is to adjust his match string accordingly.
DateTime time = DateTime.ParseExact(re, "ddd MMM d HH:mm:ss yyyy", CultureInfo.CurrentCulture);

C# How to convert irregular date and time String into DateTime?

I have a program which converts a irregular date and time string into a system DateTime.
However as the system does not recognize irregular strings, the method .ParseExact, toDateTime and TryParse has not worked.
There are only 2 types of date time strings that the program needs to convert:
Thu Dec 9 05:12:42 2010
Mon Dec 13 06:45:58 2010
Please note that the single date has a double spacing which I have used the .replace method to convert the single date to Thu Dec 09 05:12:42 2010.
May someone please advise on the codes? Thanks!
The codes:
String rb = re.Replace(" ", " 0");
DateTime time = DateTime.ParseExact(rb, "ddd MMM dd hh:mm:ss yyyy", CultureInfo.CurrentCulture);
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
I would really avoid regex and use what's already built-in .NET (TryParseExact method and date formats):
DateTime result;
string dateToParse = "Thu Dec 9 05:12:42 2010";
string format = "ddd MMM d HH:mm:ss yyyy";
if (DateTime.TryParseExact(
dateToParse,
format,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
out result)
)
{
// The date was successfully parsed => use the result here
}
You should capture the parts of your datetime into capture groups in the Match Object, then reconstitute them any way you want.
You can use this Regex statement with named groups to make it easier
((?<day>)\w{3})\s+((?<month>)\w{3})\s+((?<date>)\d)\s((?<time>)[0-9:]+)\s+((?<year>)\d{4})
This is sample code which you can try:
var str = "Thu Dec 9 06:45:58 2010";
if (str.IndexOf(" ") > -1)
{
str = str.Replace(" ", " ");
DateTime time = DateTime.ParseExact(str, "ddd MMM d hh:mm:ss yyyy", null);
}
else
{
DateTime time = DateTime.ParseExact(str, "ddd MMM dd hh:mm:ss yyyy", null);
}

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