I am trying to read non regular time format from excel in C#, the time value in excel is as "29-Aug-01 11.23.00.000000000 PM", and in excel the cells format is 'regular' not 'time'.
Now I need read the time in excel then assign the time into calendar time in asp.net/c#, how can I let the program understand the time format?
Big thx!
my code does not work
DateTime expTime = DateTime.ParseExact(strDate, "dd-MMM-yy hh.mm.ss.fffffffff tt", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
I have similar solution, but more accur.
string s = "29-Aug-01 11.23.00.000000000 AM";
DateTimeOffset myDate = DateTimeOffset.ParseExact(
s,
"dd-MMM-yy hh.mm.ss.fffffff00 tt",
System.Globalization.CultureInfo.InvariantCulture);
EDIT:
You can't use more then 7 'f' chars.
The original format string fails because the DateTime type can't represent nanoseconds. The following format string will work:
var expTime = DateTime.ParseExact(strDate, "dd-MMM-yy hh.mm.ss.fffffff00 tt",
CultureInfo.InvariantCulture);
This works because the 00 characters are interpreted as literals and ignored during parsing.
Note that I'm passing CultureInfo.InvariantCulture instead of the system's UI Culture. This ensures that the English month abbreviations will be used even if the user's UI Culture is set to another language. Otherwise, DateTime.ParseExact will try to parse the date string using the user's language month abbreviations
Related
The following code will output 2018-10-03 16:40:50 (instead of 2018-03-10) :
DateTime dateTime = new DateTime();
DateTime.TryParse("10/03/2018 4:40:50 PM", out dateTime );
MessageBox.Show(dateTime.ToString());
The following code will parse the date correctly (2018-03-10)
dateTime = DateTime.ParseExact("10/03/2018 4:40:50 PM", "dd/MM/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
MessageBox.Show(dateTime.ToString());
Is there any way to correctly parse this date without knowing the exact format?
The current culture of the server application is en-ca (English Canada) and I don't know the exact format of the string date to parse. at least is there a way to parse the date without the curious swap between the month and day? Sounds like an easy question but lost a lot of time reading and tried almost anything found here.
Thanks
End up using TryParseExact with an array list of known format. (ugly but it work)
There is no way to detect MM-DD-YYYY versus DD-MM-YYYY. Known limitation (this format is finally not supported by our application)
Thanks!
As title suggests, what I want to do is to convert my date-string e.g.
"6/6/2014 12:24:30 PM" (M/d/yyyy h:mm:ss tt) format
to
"YYYY-MM-DDThh:mm:ss.SSSZ" format.
I am trying in the following way. It's not giving me any exception, but I am getting the value like :
"YYYY-06-DDT12:24:30.SSSZ"
How can I exactly achieve this?
string LastSyncDateTime = "6/6/2014 12:24:30 PM";
DateTime dt = DateTime.ParseExact(LastSyncDateTime, "M/d/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
string result = dt.ToString("YYYY-MM-DDThh:mm:ss.SSSZ");
Data time Formats Can't recognize, the non Case sensitive chars in some systems due to their internal settings. Please refer the below code, which works fine
string result = dt.ToString("yyyy-MM-ddThh:mm:ss.SSSZ");
The above code will result as 2014-06-06T12:24:30.SSSZ
EDIT :
The below snippet will give you milliseconds as well
dt.ToString("yyyy-MM-ddThh:mm:ss.fffZ");
The simplest way is to use the "o" date formatter, like this:
dt.ToString("o");
This method will give you a timestring in the ISO 8601 format, something like this:
2014-12-25T11:56:54.9571247Z
but, since that ISO 8601 uses more than 3 decimal digits to define the second, if you only want to stop at milliseconds you can use the full formatting string and write down ss.fffzzz at the end, like this:
dt.ToString("yyyy-MM-ddThh:mm:ss.fffzzz");
and the result will be:
2014-12-25T11:56:54.957Z
For more informations you can refer to THIS LIST of date formatting options.
but I am getting the value like "YYYY-06-DDT12:24:30.SSSZ"
Let me explain why you get this result. Since there is no custom date and time format as YYYY, DD, SSS or Z, these characters are copied to the result string unchanged.
I strongly suspect you want to use yyyy, dd, fff and z format specifiers instead.
You can use DateTime.TryParseExact method like;
string s = "6/6/2014 12:24:30 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "M/d/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-ddThh:mm:ss.fffz"));
}
Here a demonstration on Ideone.
Result will be 2014-06-06T12:24:30.000+3 on my machine because my time zone is UTC +3 right now. Time zone information might change based on your UTC value as well.
Try this -
dt.ToString("o");
dt.ToString("O");
you can see this link
hope this helps
You can use date in this format :("yyyy-MM-ddThh:mm:ss.000Z")
Or in manually by changing the time zone you can easily use the date set time zone to (GMT:000(Greenwich mean time)) in your setting tab and make sure data type is date/time.
Use yyyy for Year and dd for date
string result = dt.ToString("yyyy-MM-ddThh:mm:ss.SSSZ");
Here is the quick solution:
DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffZ")
How to populate C# DateTime object from this "03-06-2012 08:00 am" string.
I'm trying some code of follwoing type:
DateTime lectureTime = DateTime.Parse("03-06-2012 08:00 am");
I am using jQuery based this http://trentrichardson.com/examples/timepicker/ plugin to generate date time.
Update --
So many answers below and lot of stuff to clear basics for this small issue
From the below snapshot u can see what I tried and what i received during debugging in visual studio
string lectureTime = "03-06-2012 08:00 am";
DateTime time = DateTime.ParseExact(lectureTime , "dd-MM-yyyy hh:mm tt", CultureInfo.InvariantCulture);
dd: days [00-31]
MM: months [00-12]
yyyy: years [0000-9999]
'-': these are separated with a dash
hh: hours [00-12]
mm: minutes[00-60]
tt: time [am, pm] (case insensitive)
If you have the correct culture, your code works without modification. But you may be using a different date formatting from the program that generated the string.
I'd recommend always specifying a CultureInfo when:
Parsing a DateTime generated by another system.
Outputting a DateTime that will be parsed by another system (not just shown to your user).
Try this:
CultureInfo cultureInfo = new CultureInfo("en-GB"); // Or something else?
DateTime lectureTime = DateTime.Parse("03-06-2012 08:00 am", cultureInfo);
See it working online: ideone
Difference between DateTime.Parse and DateTime.ParseExact
If you want .NET to make its best effort at parsing the string then use DateTime.Parse. It can handle a wide variety of common formats.
If you know in advance exactly how the dates should be formatted, and you want to reject anything that differs from this format (even if it could be parsed correctly and without ambiguity) then use DateTime.ParseExact.
You need to use DateTime.ParseExact. Something like
DateTime lectureTime = DateTime.ParseExact("03-06-2012 08:00 am", "dd-MM-yyyy hh:mm tt", CultureInfo.InvariantCulture);
I have a string which needs to be converted and validated to a DateTime. The string is in the following format 'dd.mm.yy'
I am trying to convert it to DateTime using the following
string format = "dd.mm.yy";
date = DateTime.ParseExact(current.Substring(aiRule.AiLength), format,
CultureInfo.InvariantCulture);
but unfortunately this fails.
The question is how to convert a string in the format 'dd.mm.yy' to a DateTime ?
Thank you
mm means "minutes". I suspect you want "dd.MM.yy". See MSDN for more information about custom date and time format strings.
(In particular, read the part about the "yy" specifier and how it chooses which century to use. If you can possibly change the input to use a four digit year, that could save you some problems...)
the string format should be like this....
string Format = "dd.MM.yy"
mm is for showing minutes
MM is for showing months..
I hope it will helps you...
As earlier posts has already pointed out, mm means minutes and MM means months. I ran this test snippet and it works as expected:
string format = "dd.MM.yy";
string date = "27.10.11";
DateTime result;
result = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);
I'll tell something "heretical". If dd.MM.yy (with 2 or 4 yy) is the format of your local culture, then you could let the DateTime.Parse (not ParseExact!) do its work without setting it to CultureInfo.InvariantCulture, or perhaps setting it to your local culture like new CultureInfo("it-IT").
I am trying to parse a DateTime in C# and have the following lines of code:
string dt =Convert.ToString( DateTime.FromFileTime(e8.sts[counter8].TimeStamp));
string format = "dd-MM-yyyy HH:mm:ss";
DateTime dateTime = DateTime.ParseExact(dt, format,CultureInfo.InvariantCulture);
When I debug dt is coming in as 05/18/2011 09:25:17 AM but I get an expection saying:
String was not recognized as a valid
DateTime.
Starting off, you have no need for the conversion.
DateTime.FromFileTime(e8.sts[counter8].TimeStamp) returns a DateTime already...
Even so, with the string you have provided, DateTime.Parse(str) will take care of you.
If you end up storing this value in a text file, and really are dead-set on using a custom format string to parse it (which you don't need to):
The format you have:
Day/Month/Year 24-hour:minute:second
But looking at your input date:
05/18/2011 09:25:17 AM
You want:
Month/Day/Year 12-hour:minutes:seconds AM/PM
The format for what you want is:
MM/dd/yyyy hh:mm:ss tt
Isn't this expected? Date 05/18/2011 09:25:17 AM doesn't match your format string dd-MM-yyyy HH:mm:ss. Your date is in format MM/dd/yyyy HH:mm:ss tt.
Try this:
DateTime dateTime = DateTime.Parse("05/18/2011 09:25:17 AM");
I don't see any reason for the conversion. Just use:
DateTime.FromFileTime(e8.sts[counter8].TimeStamp)
Your DateTime is coming in as MM-dd-yyyy but you are trying to parse it as dd-MM-yyyy
Change your format string to "MM-dd-yyyy HH:mm:ss tt"
You can tell this as dt, using your current format string, is trying to be parsed as the 5th day (dd) of the 18th month (MM) of 2011 (yyyy)...
EDIT:
Sorry, I completely missed the AM/PM designator, you need the tt part of the format string. This will handle the AM/PM part of the string
EDIT 2:
As per your most recent comment, you want to convert it into MM-dd-yyyy HH:mm:ss string, the all you need to do is:
var outputString = DateTime.FromFileTime(e8.sts[counter8].TimeStamp).ToString("MM-dd-yyyy HH:mm:ss");
You already have the TimeStamp in a val;id .NET DateTime object, so all you need to do is perform a .ToString() with the required time format.
DateTime parsed = DateTime.ParseExact(dt,"MM/dd/yyyy HH:mm:ss tt",CultureInfo.InvariantCulture);
s many of the others have explained here, the format needs to be changed. However even when I tried the formats they have suggested, I still received the same error that you did. Eventually I hit upon the right format to get successful results.
The format should be:
string format = "MM/dd/yyyy HH:mm:ss tt";
because you are specifying time pattern such as AM. If the month is given as single digit, eg: 5, then MM should be replaced with M. I used slash instead of hypen between the dates because that's how the original date had been given.