DateTime parse with custom format - c#

I am trying to parse a datetime value using this:
DateTime insertedDateTime = DateTime.ParseExact(tokens[0] + " " + tokens[1], "yyyy-MM-dd mm:hh:ss", CultureInfo.InvariantCulture);
//tokens[0] = 2013-09-05
//tokens[1] = 07:23:32
I am getting this error:
String was not recognized as a valid DateTime.
Any help would be appreciated.

you should write:
DateTime insertedDateTime = DateTime.ParseExact(tokens[0] + " " + tokens[1], "yyyy-MM-dd mm:HH:ss", CultureInfo.InvariantCulture);
because hh means 12h time and HH means 24h time and putting 23 as hour in 12h time is invalid :)
Of course if you are sure that hours are second in your time and you don't want to write HH:mm:ss or hh:mm:ss (for 12h format)
DEMO here

Hours should go first: "yyyy-MM-dd hh:mm:ss"
NOTE: Consider to use 24-hour HH format instead of 12-hour hh format.

You should change your mm:hh:ss to hh:mm:ss because you giving string hour part first.
DateTime insertedDateTime = DateTime.ParseExact(2013-09-05 07:23:32, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(insertedDateTime);
Output will be;
9/5/2013 7:23:32 AM
Here a DEMO.
For more informations;
Custom Date and Time Format Strings

try to use
string strdate= "yourdate";
DateTime.ParseExact(strdate, "M/d/yyyy hh:mm", CultureInfo.InvariantCulture);

Related

How do I parse "17/10/2016 6:52:13" into a DateTime

I'm trying to parse the datetime: 17/10/2016 6:52:13 as text to DateTime but I always get 01/01/0001 as output.
My code is:
DateTime.TryParseExact(dateTime, "dd/MM/yyyy" + " " + "hh:mm:ss",
CultureInfo.InvariantCulture, DateTimeStyles.None, out timeStamp);
Any clue?
Your input only has one digit for the hour, but your format string uses two. Also, since you don't have an AM/PM specifier, you probably intend to use 24-hour time, which requires a capital H.
Try "dd/MM/yyyy H:mm:ss" instead.
See this document for more details.
You can do something like this.
string givenDate = "17/10/2016 06:52:13";
DateTime myDate = DateTime.ParseExact(givenDate, "dd/MM/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
or you could simply use the "h:mm:ss" considering your given time was 6:52:13

Convert string to Datetime format dd-MMM-yyyy issue

I am getting the account expiry date of the employee from database in a string variable and the values which is in this string variable is in the format : 6/26/2016 9:14:03 AM. Now i want this format to be converted in dd-mmm-yyyy hh:mm:ss am/pm.How can i resolve this.
I tried the following code :
DateTime passexpiredate = DateTime.ParseExact(app_user.GetPasswordExpiry(empCode), "dd-MMM-yyy hh:mm tt", null);
lblPassExpiry.InnerText = "Password Expiry Date: "+passexpiredate.ToString();
Life is to easy using DateTime.ParseExact:
DateTime.ParseExact("12/02/21 10:56:09 AM", "yy/MM/dd HH:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MMM-yyyy HH:mm:ss tt");
You've to pass the required date time format in ToString() method as mentioned below:
DateTime passexpiredate = DateTime.Parse(app_user.GetPasswordExpiry(empCode));
lblPassExpiry.InnerText = "Password Expiry Date: " + passexpiredate.ToString("dd-MMM-yyy hh:mm tt");
You need to make DateTime from string 6/26/2016 9:14:03 AM. And then format it to dd-mmm-yyyy hh:mm:ss am/pm
DateTime.ParseExact requires you to specify format exactly as it matches with string representation of datetime value. So that's not going to work for your case.
You will have to first convert string to DateTime instance and then format it while displaying.
string date = "6/26/2016 9:14:03 AM";
var dt = DateTime.Parse(date);
var dtStr = dt.ToString("dd-mmm-yyyy hh:mm:ss tt");
Console.WriteLine(dtStr); // Output: 26-14-2016 09:14:03 AM

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.

Can't remove hidden characters from string

I'm trying to remove hidden characters from a string that represents a date time. I'm using .Net Fiddle and you can see the line that tries to ParseExact fails.
Here is a snippet. Please refer to the fiddle link for working code.
var dateTime = "2015-04-14 07:30:00 PM"; //<= this throws an error from some hidden char
dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", "");
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd HH:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
The HH in the format string refers to the 24-hour clock hours, which doesn't work when using AM/PM in the format string for PM times.
Change HH to hh.
It's not an invisible character. Your use of HH conflicts with your use of tt. HH is 24 hour time, but you are using tt to interpret PM (12 hour time). Change it to hh and it works.
var dateTime = "2015-04-14 07:30:00 PM";
//dateTime = Regex.Replace(dateTime, #"[^\w:\s-]", ""); <= not needed
Console.WriteLine(dateTime);
DateTime dateWithTime = DateTime.ParseExact(dateTime, "yyyy-MM-dd hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine("OK");
You need to change HH to hh.
yyyy-MM-dd hh:mm:ss tt

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..

Categories