Problem with date type - c#

i receive this date: 9/20/2010 3:32:32 PM
i need to convert to datetime.
i try:
DateTime DateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "dd/M/yyyy", CultureInfo.InvariantCulture);
but i get error: String was not recognized as a valid DateTime.
in my computer the region is: Hebrew (Israel) dd/MM/yyyy for short date and hh:mm for short time
how to fix it ?
thank's in advance

If you're receiving "9/20/2010 3:32:32 PM" as a string, then trying to parse it as if it were in the "dd/MM/yyyy" format is clearly wrong - that's try to use a month of 20. You're also only parsing part of the string - you need to either trim your string or provide the complete format.
Try this:
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM",
"M/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Note that using this sort of strict parsing will only work if you can guarantee that that will always be the format. Where are you getting this data from?

How could it work man.You are converting into "dd/MM/yyyy " & putting month as 20.In your
question dd/M/yyyy is wrong.It will like dd/MM/yyyy.
By default format is MM/DD/yyyy.
simple way to do .......
DateTime DateFrom = DateTime.Parse("9/20/2010 3:32:32 PM");
if you want to provide a specific Format so use like that
DateTime DateFrom = DateTime.ParseExact("20/09/2010 3:32:32 PM", "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
I hope it works.

It looks like your original date string is in a US format (i.e. m/dd/yyyy). Try replacing your third parameter with new CultureInfo("en-US")

DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Works for me

I wouldn't use ParseExact() when I know that time string is formatted by invariant culture.
DateTime dateFrom = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
is both more compact and more clear.

Related

Convert a string has datetime to DateTime

I want to convert a string of date and time to DateTime structure, but it is giving this error :
String was not recognized as a valid DateTime
DateTime dt = Convert.ToDateTime("5/15/2018 11:54:18 AM");
string date= dt.ToString("HH:mm");
I'm reading this question but I can't solve this code. What is my mistake?
What is the difference between Convert.ToDateTimeand DateTime.ParseExact() in C#?
Based on all the comments, here's how your code should look like
DateTime dt = DateTime.ParseExact("05/15/2018 11:54:18 AM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
string date = dt.ToString("HH:mm");
What is my mistake?
You Mistake is You are Providing argument Convert.ToDateTime() in a wrong Format.
try Providing "DD/MM/YYYY HH:MM:SS" according to Your system date Time Format .Else you need to use TryParseExatct with Specifying Format

How to converting in to time?

I am trying to convert to time from a string
My string are like this "11:45 AM" or "03:19 PM" and i am using
dateTime = DateTime.ParseExact("11:45 AM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture);
Then it is getting converted but when i am passing
DateTime.ParseExact("3:19 PM", "H:mm tt",
System.Globalization.CultureInfo.InvariantCulture).ToString();
Getting error as
String was not recognized as a valid DateTime.
I cant understand why it is happening any one have idea then please help me
I would use h instead of H. H is for the 24hr fromat, h for the 12hr format.
DateTime.ParseExact("9:45 PM", "h:mm tt", System.Globalization.CultureInfo.InvariantCulture)
See the full list of format options here.
As you want to parse the 12 hr format and convert it to the 24 hr format then you can just use this
string dt = DateTime.ParseExact("3:19 PM", "h:mm tt",CultureInfo.InvariantCulture).ToString("HH:mm");;
Unfortunately, none of the answers are completely correct.
Ante meridiem and post meridiem belong to the 12-hour clock format. That's why you should never use 24-hour clock format specifiers if your string contains one of them.
That's why you need to use h or hh specifiers, not H or HH. Since your hour part can be with leading zeros, using hh specifier is the best option for both of your string types.
Using the hh:mm tt format will parse your strings successfully.
string s = "03:19 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 15:19:00
}
and
string s = "11:45 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "hh:mm tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.Dump(); // 29.05.2015 11:45:00
}
Also, you mentioned the 3:19 PM string in your code example. Since the hour part is single digit, you need to use the h:mm tt format in that case.
Invariant culture requires two-digit hours.

convert string to datetime format invalid System.Datetime

I have been trying many different solutions found here but none works. I want to convert the string to the format of dd/MM/yyyy
editField["ExpiryTime"] = "5/19/2011 12:00:00 AM";
DateTime dt = DateTime.ParseExact(editField["ExpiryTime"].ToString(), "dd/MM/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
But I always get an error of invalid System.DateTime. Pleaes help!
Use CultureInfo.InvariantCulture to avoid culture issues like invalid date separators and this format:
M/dd/yyyy hh:mm:ss tt
Uppercase M is for months, dd are the days, yyyy the four digit years. Lowercase hh are the hours in 12h format(required in combination with AM/PM), mm are the minutes, ss the seconds and tt the AM/PM designator.
string input = editField["ExpiryTime"].ToString(); // "5/19/2011 12:00:00 AM"
DateTime dt = DateTime.ParseExact(input, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
I want to convert the string to the format of dd/MM/yyyy
Then use ToString in the same way, CultureInfo.InvariantCulture forces / as date separator, without it will be replaced with your current culture's date-separator:
string result = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you need it as string, then you should try this
var dt = string.Format("{0:dd/MM/yyyy}",DateTime.Now);
Note: Also check your local system date time format. If it mismatches with the used one , still you might experience the same exception..

Parsing a string to DateTime, formatting, then back to string - C#

I'm pulling a date/time from a MS SQL Server 2008 db and trying to format the date to show just the date in "dd/MM/yyyy" format.
The data in the DB looks like this:
2011-05-04 15:50:00.000
The unformatted string when displayed appears as this:
5/25/2011 8:47:00 AM
Yet this code fails when I try to parse it to the correct format:
DateTime dateA = DateTime.ParseExact(curShopDate, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();
I also tried this code, trying to split just the date portion away from the time:
string[] stringA = curShopDate.Split(' ');
DateTime dateA = DateTime.ParseExact(stringA[0], "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
curShopDate = dateA.ToString();
Both versions crashed with an "String was not recognized as a valid DateTime." error.
The issue is with your format parameter. Your string is not in the ddMMyyyy format, it's in the M/dd/yyyy format:
string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate.Split(' ')[0], "M/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
You could also parse the string without stripping the time from the date:
string curShopDate = "5/25/2011 8:47:00 AM";
DateTime dateA = DateTime.ParseExact(curShopDate, "M/dd/yyyy h:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture);
According to MSDN:
The DateTime.ParseExact(String, String, IFormatProvider) method parses
the string representation of a date, which must be in the format
defined by the format parameter. It also requires that the and
elements of the string representation of a date and time appear
in the order specified by format, and that s have no white space other
than that permitted by format.
So, if I'm reading that correctly, you specified the format as "ddMMyyyy" but your string is in "M/dd/yyyy h:mm:ss tt". Try either changing your format to "M/dd/yyyy h:mm:ss tt" or switch to DateTime.TryParse().
If you use ParseExact, then you must specify the exact format: "M/d/yyyy h:mm:ss tt", which matches your date string "5/25/2011 8:47:00 AM".
You can take the date component of a date/time with:
DateTime dateTime = DateTime.Now;
DateTime dateOnly = dateTime.Date;

Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C#

I am below code in c#, where I am converting my string type format date to datetime, but giving the error.
if (!string.IsNullOrEmpty(SessionDictionary.GetValue("UserDetails", "ExpiryDate")))
{
DateTime ExpiryDate = DateTime.ParseExact(SessionDictionary.GetValue("UserDetails", "ExpiryDate"), "dd mmm yy", null);
strDate = sitedata.FormatDate(ExpiryDate, TridionDateFormat.ShortDate);
}
else
{
strDate = "-";
}
My SessionDictionary.GetValue("UserDetails", "ExpiryDate") is string type data which returns "31/01/2011 00:00:00" format date, in above code where I am using DateTime.ParseExact it is giving me System.FormatException: String was not recognized as a valid DateTime. error.
Please suggest what is wrong.
Thanks.
The sample date you describe (31/01/2011 00:00:00) looks like a format dd/MM/YYYY HH:mm:ss, so why are you using dd mmm yyyy?
Try
DateTime.ParseExact(..., "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Note the use of HH (24-hour clock) rather than hh (12-hour clock), and the use of InvariantCulture because some cultures use separators other than slash.
For example, if the culture is de-DE, the format "dd/MM/yyyy" would expect period as a separator (31.01.2011).
Probably because of mmm(there isn't such month format), try MMM instead.(looks like Feb/Jan/etc)
You are using the wrong format to parse the date. The correct one is:
DateTime ExpiryDate = DateTime.ParseExact(SessionDictionary.GetValue("UserDetails", "ExpiryDate"), "dd/MM/yyyy hh:mm:ss", null)
Also, if your system date format is set to dd/MM/yyyy you can simply use:
DateTime ExpiryDate = DateTime.ParseExact(SessionDictionary.GetValue("UserDetails", "ExpiryDate"), "G", null)
Try
DateTime ExpiryDate = DateTime.ParseExact(SessionDictionary.GetValue("UserDetails", "ExpiryDate"), "g", null);
or see here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Categories