I've encountered a problem in which I am looking for a good solution.
Consider I have a node that passes data with corresponding date with the format of yyyy-MM-dd
For example: OK 2020-08-23 (Time is omitted)
And in the destination node I do parse Date by simple code
Convert.ToDateTime(date)
Recently I we noticed that destination node not properly handles date and looks it has Ambiguity in parsing date correctly
For example : if I pass data Like OK 2020-08-23 it works correct as because it knows that number of months never exceeds from 12 so 23 is day and 08 is month. However, when data is OK 2020-02-03 it don't know 02 is month or 03?
How can I resolve this Ambiguity in a proper manner?
Try using the ParseExact method and specify a custom format you need to parse:
DateTime.ParseExact(theDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
More information here.
The method has an overload where you can pass a format provider
var dtFormat = new CultureInfo("en-US", false).DateTimeFormat;
string result = Convert.ToDateTime("12/01/2011", dtFormat);
Related
In a C# application, I have to parse an xml file which is an export of a lotus notes database.
The exports contains dates in this format :
<noteinfo noteid='6706' unid='6B6A3ADD41061773C12580F2004E8EB3' sequence='6'>
<created>
<datetime dst='true'>20170329T161803,39+02</datetime>
</created>
<modified>
<datetime>20171108T100439,39+01</datetime>
</modified>
<revised>
<datetime>20171108T100439,38+01</datetime>
</revised>
<lastaccessed>
<datetime>20171108T100439,38+01</datetime>
</lastaccessed>
<addedtofile>
<datetime dst='true'>20170329T163711,21+02</datetime>
</addedtofile>
</noteinfo>
I have to extract these dates into a .Net Datetime value.
However, I don't get what's this format. Trying to parse the date fails:
var created = DateTime.Parse("20170329T161803,39+02");
Throws
String was not recognized as a valid DateTime
How to parse this date format ?
The first part of the date, before the comma, is obvious. The second part is less. +02 probably match the GMT+02 timezone, but I don't get the 39
PS: I don't have control on the exports
Assuming ,39 are the milliseconds
var created = DateTime.ParseExact("20170329T161803,38+02", "yyyyMMddTHHmmss,ffz", CultureInfo.InvariantCulture);
Reference: DateTime.ParseExact
Try
var created = DateTime.ParseExact("20170329T161803,39+02".Remove(15), "yyyyMMdd'T'HHmmss",CultureInfo.InvariantCulture);
I'm not sure what the ,39+02 is in terms of a time - zone perhaps? I trimmed it off, but if you can describe what it is maybe it can be parsed
Edit:
Assuming that's milliseconds and timezone:
var created = DateTime.ParseExact("20170329T161803,39+02", "yyyyMMdd'T'HHmmss','ffz",CultureInfo.InvariantCulture);
I've created a webtest and have a CSV data source that contains a column with a list of short dates (MM/dd/yyyy)
I need to manipulate the parameter due to part of the web page I'm testing has a form parameter that needs it to be formatted as yyyyMMdd
When the date that is captured from the data source (ex: 02/12/2016), I noticed in the Context tab of my test run that the format to "2/12/2016 12:00:00 AM"
I've created a Request plug-in and added the following code:
public override void PreRequest(object sender, PreRequestEventArgs e)
{
base.PreRequest(sender e)
string CSVDate = e.WebTest.Context["<datasource date column>"].ToString();
DateTime dt = DateTime.ParseExact(CSVDate, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
e.WebTest.Context.Add("NewDate", dt.ToString("yyyyMMdd"));
}
This generates a String was not recognized as a valid DateTime error. I tried changing the format to MM/dd/yyyy, but I encountered the same error.
Does anyone know how the correct DateTime format I should be using?
The date-time as shown in the context is 2/12/2016 12:00:00 AM. This has only one digit for the month whereas the format specifier has MM which wants two digits. The date-time also contains the letters AM that are not matched by the format.
Modifying the format to be M/dd/yyyy HH:mm:ss matches the date-time 2/12/2016 12:00:00, but does not match the AM part. In theory the tt format specifier should match this, but it did not work for me.
Rather than using ParseExact you can use Parse and it works out the correct format. Using the following worked on the date-time string provided:
DateTime dt1 = DateTime.Parse(CSVDate, new System.Globalization.CultureInfo("en-US"));
The CultureInfo is needed because the input date has the month and the days the wrong way around.
However, the real problem is in the way CSV files are handled by the web test. It appears to read them using the same logic as Microsoft Excel uses when reading CSVs. Namely, if it looks like a date then convert it to a date. So any string matching dd/dd/dddd (where d is a digit) might be connverted to a date. (E.g. 15/15/2017 will not be converted because 15 is not a month number.) I recommend rewriting the CSV to format the input date differently, use something that Excel would not treat as a date. One option is to have the date in three columns of the CSV, so have explicit day,monthandyearcolumns. Another option is to add non-date characters to the string and format it correctly, eg asz20160212and then remove thezwithin the web test. Generally, I would advise to avoid the conversion of string toDateTime` then another conversion to a different string.
I have the string "06-03-2016T06:42:44.252Z" and I would like to convert it to a datetime.
The top answer from this post suggested using:
DateTime.Parse(string, null, System.Globalization.DateTimeStyles.RoundtripKind);
This works if I format my date like "2016-06-03T06:42:45Z" but not "06-03-2016T06:42:44.252Z"
How can I convert "06-03-2016T06:42:44.252Z" to datetime properly?
Thank you very much for your time. Please let me know if I am being unclear or if you need anything else from me.
I could not find another question on stack asking how to convert from this exact format and could not apply the strategies from them to my case. I can transform my string to match those used in the example I linked but I am losing a bit of precision and adding more work in the process. I would like to leave this question up and unmarked as a duplicate in hopes of finding a means of parsing my date format or confirming that it cannot be done.
Top answer you linked pointing to ISO8601 format but your string is not on that format. Since the Z in your input indicates a UTC time, I would suggest using DateTime.ParseExact, where you can specify the exact format you want and preserve the UTC time with AdjustToUniversal style:
var dt = DateTime.ParseExact(
"06-03-2016T06:42:44.252Z",
"MM-dd-yyyyTHH:mm:ss.fffZ",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
Console.WriteLine(dt); // June 03 2016 06:42 (...)
Console.WriteLine(dt.Kind); // Utc
I noticed quite an interesting error when parsing some times.
DateTime fails to parse 24:00:00. Under some Googling and Stacking, I found out that DateTime only recognizes 00 - 23 (what the?????), so if your input is 24:00:00, you're out of luck. You would think someone would put in a condition to equate 24:00:00 as 00:00:00 (the midnight), but not yet..
My question is, how do I allow DateTime to allow me to parse 24:00:00?
Unfortunately I cannot to use NodaTime under specification reasons (sorry Jon. I love your library though).
Experimentation below:
An input of 2014-03-18 24:00:00 would present the following error. Expected.
An input of 2014-03-18 23:59:59 would successfully parse. Expected.
An input of 2014-03-19 00:00:00` would successfully parse. Expected.
There is no "24th hour" support in the DateTime class.
The hour (HH/H, 24-hour clock) must be 0-23, inclusive. This is why 00:00:00 is valid, but 24:00:00 is not.
Change 24:00:00 to 00:00:00 (before parsing) and, if needed, advance the day as appropriate (after parsing).
The following will work on times in the provided format (but only up to the 24th hour) although it doesn't account for an arbitrary format. Supporting different format strings only adds additional complications.
DateTime ParseWithTwentyFourthHourToNextDay (string input) {
var wrapped = Regex.Replace(input, #"24:(\d\d:\d\d)$", "00:$1");
var res = DateTime.ParseExact(wrapped, "yyyy-MM-dd HH:mm:ss", null);
return wrapped != input
? res.AddDays(1)
: res;
}
24:00:00 doesn't exist. It is 00:00:00 - 23:59:59
Why would you like to parse 24:00:00 as a valid time expression when it would be like saying 09:05:60. The roof for time is 23:59:59.99999999999 and after that, it turns over to 00:00:00.
Before parsing, do a simple search and replace - replace '24:00:00' with '00:00:00' and then parse as usual.
Convert to Minute.
if t.TotalMinutes < 0
double _24h = 0;
_24h = 1440 + t.TotalMinutes;
TimeSpan t = TimeSpan.FromMinutes(_24h);
ive got 2 strings, date:"27.03.11 " and time:"15:04", which id like to format as a PubDate elemnt for a rss file like Fri, 18 Nov 2005 19:12:30 GMT.
How can i do this in c sharp?
Use the following steps:
Parse the date and time strings into one DateTime variable. Use the DateTime.ParseExact static method for this.
Convert the datetime to GMT using the methods of the TimeZone class (if desired---I don't think this is mandatory according to the RSS specification).
Format this variable into a string using the DateTime.ToString method. The following MSDN pages will help you choose the correct format string based on your needs:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Since RSS requires dates to be in the RFC 822 format, the following related SO question might help you with the last step:
How do I parse and convert DateTime’s to the RFC 822 date-time format?
EDIT: For the first step, have a look at this example:
var s = "27.03.11 15:04";
var dtm = DateTime.ParseExact(s, #"dd.MM.yy HH\:mm", null);
(The \: ensures that : is seen as a literal : rather than a culture-specific time separator.)