Scope:
I have been trying to develop a super-tolerant DateTime.Parse routine, so I decided to give most "widely-used" formats a try to better understand the format masks.
Problem:
I have defined a specific format (String) which I use as myDate.ToString(format), and it works wonders. The problem is, If I get this same String (result of the .ToString(format) operation), and feed it back to DateTime.TryParseExact (...) it fails.
Code / Test:
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
// Defining Format and Testing it via "DateTime.ToString(format)"
string format = "MM/dd/yyyy HH:mm:ss tt";
string dtNow = DateTime.Now.ToString (format);
Console.WriteLine (dtNow);
// Trying to Parse DateTime on the same Format defined Above
DateTime time;
if (DateTime.TryParseExact (dtNow, format, provider, System.Globalization.DateTimeStyles.None, out time))
{
// If TryParseExact Worked
Console.WriteLine ("Result: " + time.ToString ());
}
else
{
// If TryParseExact Failed
Console.WriteLine ("Failed to Parse Date");
}
Output is : "Failed to Parse Date".
Question:
Why can I use the format string to format a certain date as text, but I can't use the same format to feed the string back to a date object ?
EDIT:
I have added part of my method to this example, and I would like to know why the "ParseDate" method fails to return a proper date, given that the "String" is in the right format.
Since you use DateTime.ToString() method without any IFormatProvider, this method will use your CurrentCulture settings.
That's why your
string dtNow = DateTime.Now.ToString (format);
line might generate a different string representation than MM/dd/yyyy HH:mm:ss tt format.
Three things can cause this issue;
Your CurrentCulture has a different DateSeparator than /
Your CurrentCulture has a different TimeSeparator than :
Your CurrentCulture has a different or empty string as a AMDesignator and/or PMDesignator
Since you try to parse your string with provider (which is InvariantCulture) on your DateTime.TryParseExact method, generate your string based on that provider as well.
string dtNow = DateTime.Now.ToString(format, provider);
You told your CurrentCulture is pt-BR and this culture has empty string "" as a AMDesignator and PMDesignator. That's why your dtNow string will not have any AM or PM designator on it's representation part.
Here a demonstration.
Related
How would you handle the following string value that needs to be converted to a DateTime object?
"2015/01/22 12:08:51 (GMT+09:00)"
Would like to include this as a recognized DateTime pattern. As I encounter other formats, I would like to just implement a new pattern.
Here a piece of code that will successfully parse the given string (notice DateTimeOffset rather than DateTime):
var str = "2015/01/22 12:08:51 (GMT+09:00)";
var dt = DateTimeOffset.ParseExact
(str,
"yyyy/MM/dd HH:mm:ss (\\G\\M\\TK)",
System.Globalization.CultureInfo.InvariantCulture
);
//dt now has +9:00 offset - that's correct only if GMT is provided as UTC.
More info at The Difference Between GMT and UTC
This code takes a string and converts it into a DateTime object
DateTime myDate = DateTime.Parse("2017-08-28 14:20:52,001", "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
All you need to do is create a format that matches your input. This link helps:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
For more details read this: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-convert-a-string-to-a-datetime
Here there is the official documentation of DateTime.Parse with some examples. It covers also the case of other formats
https://msdn.microsoft.com/it-it/library/system.datetime.parse(v=vs.110).aspx
Using DateTime.ParseExact is probably your best bet. It takes in an input string and an expected format string that the input should match. It will return true if the conversion was successful, and the out parameter will be the result of the conversion (result in the example below).
I was unable to get it to work without forcibly removing the "GMT" portion, but if that's acceptable to you, the code below should work.
This example takes the original input and converts it to UTC time (i.e. it adjusts the time based on your GMT value, which is to subtract 9 hours in your example):
var input = "2015/01/22 12:08:51 (GMT-08:00)";
var format = "yyyy/MM/dd H:mm:ss (zzz)";
DateTime result;
// Remove the "GMT" portion of the input
input = input.Replace("GMT", "");
if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal, out result))
{
Console.WriteLine($"'{input}' converts to {result} UTC time.");
}
else
{
Console.WriteLine($"'{input}' is not in the correct format.");
}
This example is modified from the ones on the DateTime.TryParseExact documentation.
if (!IsPostBack && !Page.IsCallback)
{
double OffsetHrs = GetTimeZoneOffsetFromCookie();
string dateFormat = ServiceManager.LocalizationService.GetString("AppHeaderTop", "DateFormat", "g");
CultureSelected CultureSelected = GetCultureSelected();
ASPxLabelCurrentTime.Text = DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat);
if (CultureSelected.CultureCode != "en-US")
{
DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat;
DateTimeFormatInfo currentDtfi = new CultureInfo(CultureSelected.CultureCode, false).DateTimeFormat;
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi).ToString(currentDtfi.ShortDatePattern); //what can i Use here ?
}
Let say Output of ASPxLabelCurrentTime.Text
for en-US culture is 11/2/2015 4:14 PM (70)
If I select specific culture I want this datetime 11/2/2015 4:14 PM (70) to appear in that specific culture format.
Your question seems unclear but I try to give a shot.
First of all, what is this (70) exactly? Where is this came from? en-US culture can't parse this string without using it in a string literal delimiter with ParseExact or TryParseExact methods. On the other hand, since you assing ASPxLabelCurrentTime.Text the result of the DateTime.Now.ToUniversalTime().AddHours(-OffsetHrs).ToString(dateFormat) code, I don't believe this (70) part is really an issue on this question.
Second, If I understand clearly, the problem seems the usage of DateTime.ToString(string) method.
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
.ToString(currentDtfi.ShortDatePattern);
// ^^^ Problem seems here
Okey let's say you successfully parse this ASPxLabelCurrentTime.Text with usDtfi culture (which is en-US), but with this .ToString(string) method, you are not using currentDtfi settings actually, you are using CurrentCulture settings when you generate formatted string representation of your DateTime.
From DateTime.ToString(String) doc;
Converts the value of the current DateTime object to its equivalent
string representation using the specified format and the formatting
conventions of the current culture.
Since we don't know what GetCultureSelected method returns exactly, it may or may not be the same culture with currentDtfi.
I strongly suspect, you can solve this problem to using that culture as a second parameter in ToString method as;
ASPxLabelCurrentTime.Text = Convert.ToDateTime(ASPxLabelCurrentTime.Text, usDtfi)
.ToString(currentDtfi.ShortDatePattern, currentDtfi);
IF this (70) is really part of on your string, you need to ParseExact or TryParseExact methods to supply exact format of it.
string s = "11/2/2015 4:14 PM (70)";
DateTime dt;
if(DateTime.TryParseExact(s, "MM/d/yyyy h:mm tt '(70)'", CultureInfo.GetCultureInfo("en-US"),
DateTimeStyles.None, out dt))
{
ASPxLabelCurrentTime.Text = dt.ToString(currentDtfi.ShortDatePattern, currentDtfi);
}
I accept date info from the user, via date picker. I need to store them in a culture neutral way. The problem I am facing is, if I store the date as per en-US format (based on calendar settings), namely 11/20/1990 it will fail to parse when the culture is en-GB.
And vice versa happens when culture is en-US, date stored as per UK format, dd/mm/yyyy refuses to parse. How do I store date info in a culture neutral way in a file so that, I get the date to work in both locations?
DateTime.TryParse(userEnteredValue, out result);
result.ToShortDateString(); //this is what I am doing
tried this code for invariant culture
string input = "20/10/1983";
DateTime userInput;
bool result = DateTime.TryParse(input, out userInput);
string invariantCulture = userInput.Date.ToString(CultureInfo.InvariantCulture);
DateTime storedValue;
result = DateTime.TryParse(invariantCulture, out storedValue);
tried this code with en-GB calendar settings, second statement DateTime.TryParse fails infact.
#Soner Gönül's answer is spot on if you are saving the dates to a database. However, you mention that you are looking to round-trip a DateTime to and from a file.
As the file is presumably a text file you'll need to write the DateTime in a culture neutral manner. You can do this by using the "O" format specified on the DateTime.ToString method. This will output a string representation that complies with ISO 8601. The resultant string can be parsed using DateTime.Parse without the need for culture information.
As an example:
string filename = #"c:\temp\test.txt";
string usDateString = "11/18/2014 12:32"; // MM/dd/yyyy
string ukDateString = "18/11/2014 12:33"; // dd/MM/yyyy
//I'm mimicking you getting the DateTime from the user here
//I'm assuming when you receive the date(s) from the front
//end you'll know the culture - if not then all bets are off.
DateTime usDate =
DateTime.Parse(usDateString, CultureInfo.GetCultureInfo("en-US"));
DateTime ukDate =
DateTime.Parse(ukDateString, CultureInfo.GetCultureInfo("en-GB"));
//write the dates to a file using the "o" specifier
File.AppendAllText(filename, usDate.ToString("o") + Environment.NewLine);
File.AppendAllText(filename, ukDate.ToString("o") + Environment.NewLine);
//read them back in as strings
string[] contents = File.ReadAllLines(filename);
foreach (var date in contents)
{
//prove we can parse them as dates.
Console.WriteLine(DateTime.Parse(date).ToString());
}
This creates a file with the contents:
2014-11-18T12:32:00.0000000
2014-11-18T12:33:00.0000000
and on my system (in the UK) it prints:
18/11/2014 12:32:00
18/11/2014 12:33:00
if I store the date as per en-US format...
Please stop! Looks like you try to save your DateTime values with their string representations.
A DateTime doesn't have any implicit format. It has just date and time values. String representations of them can have a format. Generate your insert queries and pass your DateTime values directly with parameterized way.
Please read;
Bad habits to kick : choosing the wrong data type
If you want to get string representations of your DateTime values with specific format, you can always use DateTime.ToString() method and it's overloads.
Your en-GB culture can parse MM.dd.yyyy (since you use / format specifier which replaces itself supplied culture DateSeparator) and en-US culture can parse MM/dd/yyyy as well.
But since you use .ToShortDateString() method, this represents your datetime based your CurrentCulture settings. As a solution, you can set this property which culture you want and ToShortDateString works based on it.
result = DateTime.TryParse(invariantCulture, out storedValue);
tried this code with en-UK calendar settings, second statement
DateTime.TryParse fails infact.
Because this DateTime.TryParse uses your CurrentCulture and since your invariantCulture variable is 10/20/1983 00:00:00, that means this is not a standard date and time format for your CurrentCulture.
There is no such a culture as en-UK by the way.
10/20/1983 00:00:00 is MM/dd/yyyy HH:mm:ss format. But en-GB culture doesn't have this format as a standard date and time format, that's why your method returns false.
As an alternative, you can use custom format strings like;
string s = "10/20/1983 00:00:00";
string format = "MM/dd/yyyy HH:mm:ss";
DateTime dt;
if (DateTime.TryParseExact(s, format, CultureInfo.GetCultureInfo("en-GB"),
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
I bumped into this question and figured I'd bring in some other way nobody has mentioned yet:
DateTime.ToBinary() for serializing and DateTime.FromBinary(Int64) for deserialization.
What these do is the following:
ToBinary() returns a long which can be easily stored in a culture invariant way.
FromBinary(Int64) will return a DateTime object from the long parameter supplied.
(They even take the date time Kind property into consideration).
And here's some code to go with it:
DateTime d1l = DateTime.Now;
long dl = d1l.ToBinary();
DateTime d2l = DateTime.FromBinary(dl);
DateTime d1u = DateTime.UtcNow;
long du = d1u.ToBinary();
DateTime d2u = DateTime.FromBinary(du);
Console.WriteLine("Local test passed: " + (d1l == d2l).ToString());
Console.WriteLine("d2l kind: " + d2l.Kind.ToString());
Console.WriteLine("Utc test passed: " + (d1u == d2u).ToString());
Console.WriteLine("d2u kind: " + d2u.Kind.ToString());
And the console output:
Local test passed: True
d2l kind: Local
Utc test passed: True
d2u kind: Utc
I find this to be pretty neat!
String s = "24. 11. 2001";
d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("sk-SK"));
en-AU (English Austrailia): 24/11/2001
en-IA (English India): 24-11-2001
en-ZA (English South Africa): 2001/11/24
en-US (English United States): 11/24/2001
i suspect you prefer English (India) (en-IA).
But if you really can't decide what culture to use when converting dates to strings and vice-versa, and the dates are never meant to be shown to a user, then you can use the Invariant Culture:
String s = "11/24/2001" //invariant culture formatted date
d = DateTime.Parse(s, CultureInfo.InvariantCulture); //parse invariant culture date
s = d.ToString(CultureInfo.InvariantCulture); //convert to invariant culture string
I tried to figure out a solution via this approach, please let me know if its correct.
The code which I use is below.
For Saving date time I use ticks as below.
DateTime userInput;
bool result = DateTime.TryParse(this.dpSave.Text, out userInput);
if (result)
{
long ticks = userInput.Ticks;
System.IO.File.WriteAllText(#"D:\folder\Ticks.txt", ticks.ToString());
}
else
{
MessageBox.Show("Date time parse failed");
}
For loading it back, I use
if (System.IO.File.Exists(#"D:\folder\Ticks.txt"))
{
string contents = System.IO.File.ReadAllText(#"D:\Sandeep\Ticks.txt");
long ticks;
if (long.TryParse(contents, out ticks))
{
DateTime storedDateTime = new DateTime(ticks);
MessageBox.Show("Stored Date" + storedDateTime.ToShortDateString());
}
else
{
MessageBox.Show("Unable to obtain stored dates");
}
}
this seems to work, provided, I save using en-US culture and load using en-GB culture.
please let me know if this is the right approach!
a) Exchange data shall always be stored culture invariant (xml etc)
b)You've gotta to be careful with the terminology.
What you exactly mean is culture INVARIANT (and not 'culture neutral').
There are three types of cultures:
1) invariant
2) culture neutral (e.g. "en")
3) culture specific (e.g "en-US")
public DateTime getdate3()
{
CultureInfo Invc = CultureInfo.InvariantCulture; //culture
string cul = Thread.CurrentThread.CurrentCulture.Name;
CultureInfo us = new CultureInfo(cul);
string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern;//pattern
DateTime dt = Convert.ToDateTime(DateTime.Now);
TimeZoneInfo myZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"); //india zone
DateTime dateindia = TimeZoneInfo.ConvertTime(dt, myZone);
string dt1 = Convert.ToDateTime(dateindia).ToString(shortUsDateFormatString); //string format
}
I am getting a string and i want to parse that string as date and want to store it in DataTable.
string can be in formats
1- "2014/23/10"
2- "2014-23-10"
{
string st="2014/23/10";
string st="2014-23-10";
}
And attach time with it.
Any idea to make it possible ?
DateTime.ParseExact or DateTime.TryParseExact are appropriate here - both will accept multiple format strings, which is what you need in this case. Make sure you specify the invariant culture so that no culture-specific settings (such as the default calendar) affect the result:
string[] formats = { "yyyy-MM-dd", "yyyy/MM/dd" };
DateTime date;
if (DateTime.TryParseExact(input, formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
// Add date to the DataTable
}
else
{
// Handle parse failure. If this really shouldn't happen,
// use DateTime.ParseExact instead
}
If the input is from a user (and is therefore "expected" to be potentially broken, without that indicating an error anywhere in the the system), you should use TryParseExact. If a failure to parse indicates a significant problem which should simply abort the current operation, use ParseExact instead (it throws an exception on failure).
Since both are not standart date and time format, you can use DateTime.ParseExact method like;
string st = "2014/23/10";
string st1 = "2014-23-10";
var date = DateTime.ParseExact(st,
"yyyy/dd/MM", CultureInfo.InvariantCulture);
var date1 = DateTime.ParseExact(st1,
"yyyy-dd-MM", CultureInfo.InvariantCulture);
Output will be;
10/23/2014 12:00:00 AM
10/23/2014 12:00:00 AM
Here a demonstration.
Of course these outputs depends your current culture thread.
If you want to format your DateTime's as a string representation, you can use DateTime.ToString(string) overload which accepts as a string format.
Since you have more than one format, you can use DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) overload which is takes your formats as a string array.
var formats = new []{"yyyy-MM-dd", "yyyy/MM/dd"};
DateTime dt;
if(DateTime.TryParseExact(st, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
//
}
else
{
//
}
Convert to a DateTime with DateTime.TryParseExact(); or even DateTime.Parse if you need to be flexible. Then you can format it back out however you like!
See: http://msdn.microsoft.com/en-us/library/ms131044(v=vs.110).aspx
Try
DateTime.Parse(st, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Set DateTimeStyles based on your requirement.
Try this:
DateTime.Parse(st);
If It the above line not works for you, then add cultrureInfo below:
DateTime.ParseExact(st,"yyyy/dd/MM", CultureInfo.InvariantCulture);
I need to parse string value to date time value, I have date in this format:
DD.MM.YYYY
I want to parse value in this format:
YYYY-MM-DD
I tried to do it like this:
DateTime.ParseExact(date_req, "yyyy-MM-dd", CultureInfo.InvariantCulture);
But i have an error: String was not recognized as a valid DateTime.
Is there a way to do this?
If you have a string in the format DD.MM.YYYY why are you passing YYYY-MM-DD to your ParseExact function?
Try like this:
string dateStr = "12.06.2012";
DateTime date = DateTime.ParseExact(dateStr, "dd.MM.yyyy", CultureInfo.InvariantCulture);
Then when you want to output this DateTime instance somewhere you could use the YYYY-MM-DD format, like this:
string result = date.ToString("yyyy-MM-dd");
I think what you want to do is parse your dd.MM.yyyy and then display it as yyyy-MM-dd.
You first have to parse the string into a DateTime:
DateTime date = DateTime.ParseExact(date_req, "yyyy-MM-dd", CultureInfo.InvariantCulture);
Now date is a representation of the date that the computer actually understands (before it was just a string). You can now display this object anyway you want:
string yyyyMMdd = date.ToString("yyyy-MM-dd");
string arabic = date.ToString("yyyy-MM-dd", new CultureInfo("ar"));
// and so on
Don't forget that when converting dates from strings to DateTime and back, culture and time zones are worth keeping in mind.
When parsing a date you need to specify the format you want to read, not the format you want as output later.
So use dd.MM.yyyy as argument to ParseExact.
Check DateTime.ParseExact Method (String, String,
IFormatProvider) 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.
you have to specify format string as DD.MM.YYYY rather than "yyyy-MM-dd".
try this:
DateTime dateValue = DateTime.ParseExact(date_req, "DD.MM.YYYY", CultureInfo.InvariantCulture );
// use this when you need to show that formatted date value
string formattedDate = dateValue.ToString("yyyy-MM-dd");
Better way is that use DateTime.TryParseExact Method, if you want it as date rather than string modify your culture info and date separator.
CultureInfo enUS = new CultureInfo("en-US");
string dateString;
DateTime dateValue;
dateString = "05.01.2009";
if (DateTime.TryParseExact(dateString, "DD.MM.YYYY", enUS,
DateTimeStyles.None, out dateValue))
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
First parse it in the existing format then convert to the string format you want.
var date = DateTime.ParseExact(date_req, "dd.MM.yyyy", CultureInfo.InvariantCulture);
var str = date.ToString("yyyy-MM-dd");
You can first convert it to a character array. then you can parse day/month/year separately to integers . You know the indexes of the numbers so this will be easy. after that you can concatenate every element in the way you like.
Error. You have a cake and you want to eat a stake. In order to convince your stomach that that the cake is a stake you have to transform the cake to a stake. This cannot be done. Parsing is about accepting a value as it comes and use a pattern (or more) to translate it to something else and not transform it. So what you want may be right but you ask is wrong.