my code below throw an exception as invalid date time. the error occur after i publish it to my server. working find at my developing PC
string str = "27-07-2015 6:15 pm";
DateTime dt = Convert.ToDateTime(DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null).ToString("dd-MM-yyyy hh:mm tt"));
it takes '27' as month and '7' as day.
what i did to solve the problem:
i already update the datetime format on that server to dd-MM-yyyy
i double checked the capital and small letter of the date time format.
change the 'null' to 'CultureInfo.InvariantCulture'
change 'pm' to 'PM', 'tt' to 'TT'
read through all the resources i could find on google and stackoverflow, nothing's help.
am i missing something here? i know i did... :(
As #Rawling correctly noted, you're parsing the datetime twice: first, using your custom formatting, and second, using the system's default formatting.
This is silly - you already have the DateTime from the ParseExact method:
string str = "27-07-2015 6:15 pm";
var dt = DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null);
That's it, you're done. No need to convert to string again, and parse that once more (and even worse, using the same custom formatting to do the ToString, so the subsequent Convert.ToDateTime is bound to fail anywhere that's not the default datetime formatting).
There are a few possibilities;
Let's analyze your DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", null) part first. This will be parsed your 27-07-2015 6:15 pm string successfully if;
Your CurrentCulture's TimeSeparator is : and
Your CurrentCulture's PMDesignator is PM (not empty string)
If both are okey, you have successfully parsed a DateTime.
After that, you generate it's textual representation with dd-MM-yyyy hh:mm tt format. And that's still depends on your CurrentCulture, your result might have PM or not. For both case, there is no guaranteed to parse your string with Convert.ToDateTime method because it will be parsed your string only if it is a standard date and time format of your CurrentCulture.
On the other side, what you do doesn't make sense to me. You parse your string first, then you generate string representation of it, then you try to parse this string again. Doesn't make sense, right?
I strongly suspect you just need;
string str = "27-07-2015 6:15 pm";
DateTime dt = DateTime.ParseExact(str, "dd-MM-yyyy h:mm tt", CultureInfo.InvariantCulture);
Related
It may be asked many time but still i cannot get the solution for this i have string in format like 24-12-2016 12:24 PM how to convert it into exactly same format like this into to datetime date type so far what i have tried is:
DateTime dt = DateTime.ParseExact(callobj.CallStartTime, "yyyy-MM-dd hh:mm tt", CultureInfo.GetCultureInfo("en-US"));
// String CallStartTimeString = Convert.ToDateTime(callobj.CallStartTime);
var s = dt.ToString("d-M-yyyy hh:mm tt", CultureInfo.InvariantCulture);
DateTime datetimes = DateTime.ParseExact(s, "d-M-yyyy h:mm tt", CultureInfo.InvariantCulture);
In datetimes am getting like this 12-24-2016 12:24 PM but i need to get like this 24-12-2016 12:24 PM how can i do this ? as am new to c# can someone Helpme out Thanks in advance!! In every other question it just left till string but i need to convert it again to datetimeformat and assign to other datetime datatype
Okey, since Jon and Theodoros are quite right with their comments, I try to clear all things if you let me..
It may be asked many time but still i cannot get the solution for this
i have string in format like 24-12-2016 12:24 PM...
Honestly, based on it's name, your CallStartTime property looks like DateTime typed (well, actually it fits better with TimeSpan but that's irrelevant). But if you really sure it is string, I'm still not with you because you can't parse 24-12-2016 12:24 PM string with yyyy-MM-dd hh:mm tt format since they don't match exactly.
how to convert it into exactly same format like this into to datetime
date type
Wait a second... A DateTime does not have any implicit format. It just have date and time values. It's value stored 64-bit dateData field as Ticks (first 62 bits) and DateTimeKind (63 and 64 bits) of it. "Format" concept only applies when you try to get textual (also known as string) representation.
In datetimes am getting like this 12-24-2016 12:24 PM but i need to
get like this 24-12-2016 12:24 PM
Sorry but I don't believe in you. Since your datetimes is a DateTime, even if you look at in debugger, you can't see any time designators with it. Those are can be only part of a string when you get string representation of your datetimes.
A Datetime instance looks like this in debugger;
Since your question is not very clear, there are 2 options;
You have a string as 24-12-2016 12:24 PM and you want to parse it to DateTime.
You have a Datetime and you want to get it's string representation like 24-12-2016 12:24 PM.
If you try to solve first, you just need to use ParseExact with dd-MM-yyyy hh:mm tt format.
string s = "24-12-2016 12:24 PM";
DateTime dt = DateTime.ParseExact(s, "dd-MM-yyyy hh:mm tt",
CultureInfo.GetCultureInfo("en-US"));
If you try to solve second, you just need to use ToString method with proper format and culture settings.
string s = dt.ToString("dd-MM-yyyy hh:mm tt", CultureInfo.GetCultureInfo("en-US"));
Try This
String str = "12-24-2016 12:24 PM";
DateTime dDate = DateTime.Parse(str, CultureInfo.InvariantCulture);
Srring strDayFirst = Format(dDate, "dd/MM/yyyy");
I am trying to convert the string to DateTime. But I can not convert.
DateTime dt = DateTime.Parse("16/11/2014", CultureInfo.InvariantCulture);
Console.WriteLine("Date==> " + dt);
The error is FormatException.
My input time format is "dd/MM/yyyy".
Please let me any idea to resolve my problem.
Given that you know your input format, you should specify it with `ParseExact:
DateTime dt = DateTime.ParseExact(text, "dd/MM/yyyy",
CultureInfo.InvariantCulture);
I would always recommend being as explicit as you can be about date/time formats. It makes your intention very clear, and avoids the possibility of getting months and days the wrong way round.
As Soner has stated, CultureInfo.InvariantCulture uses MM/dd/yyyy as its short date pattern, as you can validate with:
Console.WriteLine(CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)
As a mild plug, you might want to consider using my Noda Time project for your date/time handling - aside from anything else, that allows you to treat a date as a date, rather than as a date and time...
Because InvariantCulture doesn't have dd/MM/yyyy as a standard date and time format, but it has MM/dd/yyyy as a standard date and time format.
That's why it thinks your string is MM/dd/yyyy format, but since there is no 16 as a month in Gregorian calender, you get FormatException.
Instead of that, you can use DateTime.TryParseExact method to specify exact format like;
string s = "16/11/2014";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
}
I am reading from excel file and save the content into database
one of the column contains length of video in this format
HH:mm:ss
I write this code so far
string time = oledbReader[6].ToString();
DateTime streamingTime = DateTime.ParseExact(time,
"HH:mm:ss",
System.Globalization.CultureInfo.CurrentCulture);
I am getting error:
String was not recognized as a valid DateTime.
I tried debug mode and I see the value :"30/12/1899 00:09:21" in the Variable time
when the value in the current execl column is:"00:09:21"
Where does the "30/12/1899" came from? Why is the string was not recognized as a valid DateTime?
Can I save only the format HH:mm:ss into sql server?
Try this, easy hack as my comment above.
string time = oledbReader[6].ToString().Split(" ".ToCharArray())[1];
DateTime streamingTime = DateTime.ParseExact(time, "HH:mm:ss",System.Globalization.CultureInfo.CurrentCulture);
or you could parse it as it is...
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
Since you didn't gave us any information about your CultureInfo, here with InvariantCulture;
string time = "30/12/1899 00:09:21";
DateTime streamingTime = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(streamingTime);
Output will be;
12/30/1899 12:09:21 AM
Here a DEMO.
For more informations, check out Custom Date and Time Format Strings
Use a TimeSpan structure to hold a time value. A DateTime includes both a date and a time.
The problem is that excel not have time field and automatically convert time to date time field.
If course date is "empty" which means that it its a minimal date for excel - 30/12/1899
Moreover you don't have to invoke ToString methods because object is already a DateTime
i have a string which contains date time this...
string S="08/18/2013 24:00:00"
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy HH:mm:ss", null);
i want to parse it into date time but shows an exception like this.
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.
please tell me any solution for this problem.
The problem is with the hour being 24. DateTime doesn't support this, as far as I'm aware.
Options:
Use my Noda Time project which does support 24:00:00, but basically handles it by adding a day (it doesn't preserve a difference between that and "end of previous day")
Keep using DateTime, manually replace "24:00:00" with "00:00:00" when it occurs, and remember to add a day afterwards
If you want to preserve the information that it was actually "end of the day" you'd need to do that separately, and keep the information alongside the DateTime / LocalDateTime.
You should also parse with the invariant culture as other answers have suggested - you're not trying to parse a culture-specific string; you know the exact separators etc.
string S="08/18/2013 00:00:00"; // here is the first problem occurred
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
From The "HH" Custom Format Specifier
The "HH" custom format specifier (plus any number of additional "H"
specifiers) represents the hour as a number from 00 through 23; that
is, the hour is represented by a zero-based 24-hour clock that counts
the hours since midnight.
So, using 24 as an hour is invalid on this case.
Try with hh format with 00 instead like;
string S = "08/18/2013 00:00:00";
DateTime DT = DateTime.ParseExact(S, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Here a DEMO.
If you really want to use 24:00:00 as a hour, take a look Noda Time which developed by Jon.
[Please vote to close this - see my last comment.]
Hi,
Something like this:
DateTime.ParseExact("25/12/2008 00:00:00", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
works fine on my development machine but not after deployment (server).
I guess it has to do with some time zone configuration. I have tried:
<%# ... UICulture="en" Culture="en-US" %>
with no avail. Any suggestions on a postcard please. Thanks.
Christian
PS: Exception:
String was not recognized as a valid DateTime
PPS: I have updated the question. I actually feed in the time bit. sorry about that!
PPPS:
I have now realised that all this has to do with Excel and oledb. The string 25/12/2008 looks like this "12/25/2008 12:00:00 AM" on the server and like this "25/12/2008 00:00:00" on the developement machine. I adjusted the time zone of the server to UK without avail. What else can I do? Thank and sorry about all this confusion!!!
Something like this
You'd do better posting exactly what failed, and the exact error, rather than "something like" what failed.
I would expect your sample to give a FormatException, since the string you're converting ("25/12/2008", no time) does not match the format specified ("dd/MM/yyyy hh:mm:ss").
Also a bit strange to be using hh rather than HH in your format - hh is a 12-hour clock.
I would expect any of the following to work.
// No time component
DateTime.ParseExact("25/12/2008", "dd/MM/yyyy", new CultureInfo("en-US"));
// Works for hours <=12, result is always AM
DateTime.ParseExact("25/12/2008 11:00:00", "dd/MM/yyyy hh:mm:ss", new CultureInfo("en-US"));
// Works for hours using 24-hour clock
DateTime.ParseExact("25/12/2008 13:00:00", "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US"));
When parsing a string to DateTime, always consider that the date could have one or two digits for day and month. For example, it could be MM/dd/yyyy or M/d/yyyy or MM/d/yyyy or M/dd/yyyy. If you use ParseExact and you don't consider that, you'll get an exception. Try this:
DateTime date = DateTime.ParseExact(
dateText, // date in string
new string[] { "M/d/yyyy", "MM/dd/yyyy", "M/dd/yyyy", "MM/d/yyyy" }, // formats (you can add more)
CultureInfo.InvariantCulture,
DateTimeStyles.None);
try
DateTime.ParseExact("25/12/2008", "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);