I'd like to convert a bunch of date strings like the following Mon Aug 7 15:32:52 GMT+0900 2007
to
C# datetime objects.
Is there anything built in to the .net framework to do this or will I have to parse the string into date parts?
Many thanks,
You could use:
DateTime.Parse(datestring);
or
DateTime.TryParse(string, IFormatProvider, DateTimeStyles, out DateTime)
Look at the DateTime.Parse method. You can use the DateTimeFormatInfo class as IFormatProvider. There you could specify the format of the date you want to parse.
Im not sure what "date strings like the following" means since seems you forgot to provide a example. But maybe if you try this.
string date = DateTime.Today.ToString("ddd MMM d HH:mm:ss G'M'Tzzz yyyy", CultureInfo.CreateSpecificCulture("en-EN"));
date = date.Remove(date.LastIndexOf(':'), 1);
// Do whatever you want with the date string
// Output looks like Wed Sep 9 00:00:00 GMT+0200 2009
That looks like a simple RFC formatted date, so a straight DateTime.Parse as Ikke said will work and you shouldn't have to provide the format. You can pass a DateTime object as the second argument in the DateTime.TryParse method to see whether it fails or not, as it returns a boolean.
Related
I Need help with this simple and silly thing ..
Want to be able to convert this string representation "Oct 9 2017 2:45:67:145PM" to date.
I am using code below:
string strDate = "Oct 9 2017 2:45:67:145PM";
DateTime dtTroubleDate;
dtTroubleDate = DateTime.ParseExact(strDate.ToString(), "MMM d yyyy h:mm:ss:ffftt", CultureInfo.InvariantCulture);
MessageBox.Show("dtTroubleDate String : " + dtTroubleDate.ToString());
This is a C# code within a SSIS package. I am reading the date from a file.
Need to store in the database as 'datetime2'
Never in the history of the Gregorian calendar have been a time with 67 seconds...
This must be a typo in the file itself. The format you are using is OK, but I would recommend using TryParseExact instead of ParseExact for this very reason.
When using ParseExact you are basically saying "I know the string representation of the datetime value will always be in this specific format and I will always be able to parse it.
However, that is rarely the case - as most of the time string representation of datetime values are written by fallible humans, occasionally there will be typos - and that's exactly what the TryParse methods are all about.
string strDate = "Oct 9 2017 2:45:67:145PM";
DateTime dtTroubleDate;
if(DateTime.TryParseExact(
strDate,
"MMM d yyyy h:mm:ss:ffftt",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dtTroubleDate))
{
// Datetime is valid
}
I need to convert a windows hex 64 bit (big endian) date time to something readable in c#?
example '01cb17701e9c885a'
converts to "Tue, 29 June 2010 09:47:42 UTC"
Any help would be appreciated.
Looks like you need to parse this to long as hexadecimal and use FromFileTime to generate windows file time as a local and use ToUniversalTime to generate UTC equivalent.
long number = long.Parse("01cb17701e9c885a", NumberStyles.AllowHexSpecifier);
DateTime date = DateTime.FromFileTime(number).ToUniversalTime(); // 06/29/2010 09:47:42
or simpler, use FromFileTimeUtc as Matt mentioned;
DateTime date = DateTime.FromFileTimeUtc(number); // 06/29/2010 09:47:42
If you wanna get it as a string representation, you can use ToString method that DateTime instance with english-based culture (like InvariantCulture) as;
string str = date.ToString("ddd, dd MMMM yyyy HH:mm:ss 'UTC'",
CultureInfo.InvariantCulture);
Use DateTime.FromFileTimeUtc
For detail, visit this site
There is an Oracle DB API function which expects as an input a string representing date (or in other cases number) which is formatted by the provided format mask.
Before I went and implemented Oracle Format Mask parser I thought I would ask whether there is a .net built-in or third party way of converting DateTime .net object to string using Oracle Format Mask?
Update
Examples:
DateTime date = DateTime.Now;
string convertedDate = ConvertDateTimeToOracleFormattedString (date, "Month DD, YYYY"); // should produce: March 21, 2014
convertedDate = ConvertDateTimeToOracleFormattedString (date, "CC BC"); // should produce: 21
// any other valid oracle format combination: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
My current idea is to parse provided Oracle Format Mask into equivalent .net custom date and time format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx and use it when converting .net DateTime to string: DateTime.Now.ToString ("equivalentDotNetFormat");. The equivalent .net format of "Month DD, YYYY" would be "MMMM dd, yyyy". Unless there is a quicker approach.
Could you please take a look at ODP .NET?
I believe it has the necessary classes to provide you the functionality you desire.
I am currently trying to parse a string that is obtained from an xml that is downloaded from the web every few minutes. The string looks like this:
Thu Jul 12 08:39:56 GMT+0100 2012
At first I just did a string.split and took out everything after the time (GMT+0100 2012) and inserted 2012 after the date.
This worked great until the date changed to:
Thu Jul 12 08:39:56 GMT+0000 2012
So I would like to dynamically pasre the GMT+ whatever as they send me that string in c#.
Any advice would be appreciated.
You can use DateTime.ParseExact with a custom date and time format string:
DateTime.ParseExact("Thu Jul 12 08:39:56 GMT+0000 2012",
"ddd MMM dd hh:mm:ss 'GMT'K yyyy",
CultureInfo.InvariantCulture)
This will throw a format exception if the string and format string do not match exactly, so you may want to use DateTime.TryParseExact that will return a false if it fails.
Instead of DateTime you may want to use DateTimeOffset that preserved timezone information , as #Keith commented - this may be important to your application.
Two things you can do: First, you should be able to use a custom format string with a ParseExact method, either from DateTime or DateTimeOffset (I would use DateTimeOffset if the actual time zone of the stamp is important, and not just the equivalent time in UTC or your local time zone).
Have a look: DateTime custom format string
The format string would probably be something like #"ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy".
However, there's one snag; the .NET time zone offset ("zzzz" or simply "K") always includes a colon between the hour and minute when expressed as a string, which your input strings do not have. There is no way I know of to specify that the time zone offset doesn't/shouldn't have this colon, and I'm pretty sure that trying to parse it without a colon would cause an error.
The simplest workaround is to remove that specific colon from the string prior to parsing it. The code for that given your input is simply to remove the last colon character in the string:
var updatedString = inputString.Remove(inputString.LastIndexOf(':'), 1);
Try DateTime.Parse method to parse your date.
This should work:
XmlConvert.ToDateTime(textBox1.Text, "ddd MMM dd HH:mm:ss 'GMT'zzzz yyyy");
Probably a simple question -
I'm reading in data from a number of files.
My problem is, that when I'm reading in the date from an american file, I parse it like so:
DateSold = DateTime.Parse(t.Date)
This parses the string t.Date into a date format, however it formats the american date to a european date, e.g.
If the date is in the file as 03/01/2011, it is read as the 3rd of January, 2011, when it should be the 1st of March 2011.
Is there a way of doing this so that it formats to the european date?
var dt = DateTime.ParseExact(t.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
The DateTime itself has no formatting, it is only when you convert it to or from a string that the format is relevant.
To view your date with American format, you pass the format to the ToString method
string americanFormat = dt.ToString("MM/dd/yyyy");
If you are parsing the date from a file which is specifically a US formatted file then simply pass the US culture information into the parse function as follows;
var usCulture = "en-US";
var dateValue = DateTime.Parse(dateString, new CultureInfo(usCulture, false));
This way you can simply swap out the culture string per different region required for parsing. Also, you no longer have to research the specific datetime format nuances for each culture as .Net will take care of this for you as designed.
Use DateTime.ParseExact or DateTime.TryParseExact when parsing, and specify a format string when you format with ToString too.
Note that there's no such thing as "an American date" after it's been parsed. The DateTime value has no concept of formatting.
It sounds like you're not actually interested in the Parse part so much as the formatting part, e.g.
string formatted = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
... but I would recommend that you control both the parsing and formatting explicitly.
If you have different file formats, you'll need to give different format strings when you read each file. How you then format the data is a separate decision.
If you know the format ahead of time, you can use DateTime.ParseExact, using the American format as your format string.
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012
string formatteddate=DateTime.Now.ToString("D") // output: Monday, November 08, 2012
string formatteddate=DateTime.Now.ToString("f") // output: Monday, November 08, 2012 3:39 PM
string formatteddate=DateTime.Now.ToString("g") // output: Monday, November 08, 2012 3:39:46 PM
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012 3:39 PM
More date-time format in asp.net is given here.
http://dateformat.blogspot.in/2012/09/date-time-format-in-c-aspnet.html