parse string to datetime using parse exact - c#

I am trying to parse a string to datetime using ParseExact but I keep failing..
I tried below but received an error: String was not recognized as a valid DateTime.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = new DateTime();
d = DateTime.ParseExact(topA, "dd/MM/yyyy HH:mm:ss tt", null);

Based on your string, right format should be M/dd/yyyy hh:mm:ss tt with preferable InvariantCulture.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = DateTime.ParseExact(topA, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);

Related

Not recognized valid date date time

Need to convert 08-13-2022 10:27 PM to 08/12/2022.
This is my code:
DateTime date = DateTime.Parse(String.Format("MM/dd/yyyy", row["POSTING_DATE"]));
row["POSTING_DATE"] = Convert.ToDateTime(date);
But I get this error:
not recognized valid date date time
You can use the DateTime.ParseExact method as follows:
string POSTING_DATE = "08-13-2022 10:27 PM";
DateTime date = DateTime.ParseExact(POSTING_DATE, "MM-dd-yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
POSTING_DATE = date.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(POSTING_DATE);//prints 08/13/2022

DateTime.ParseExact returns wrong date for the 'dd/MM/yyyy h:mm tt' format

I need to convert datetime from MM/dd/yyyy h:mm:ss tt format to the dd/MM/yyyy h:mm:ss tt
My code
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
In result date = 25/12/2017 12:00:00 AM
But resultDate = 12/25/2017 12:00:00 AM .
How can i parse it right?
You are looking at the object in the debugger which is a datetime object and not a display version of your date.
Example:
var theDate = DateTime.UtcNow;
var date = theDate.ToString( "dd/MM/yyyy h:mm:ss tt" );
var resultDate = DateTime.ParseExact( date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture );
Console.WriteLine(date == resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));//Returns true
As you see they are the same dates so just .ToString() it to whatever format you need when displaying.
You don't need to convert it to a String and back to a DateTime. The resulting DateTime object should have the same Date. You just need to convert it when you want to display it somewhere:
((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
here
var date = ((DateTime)model.WorkshopDate).ToString("dd/MM/yyyy h:mm:ss tt");
you are implicitly asking to display the date in "dd/MM/yyyy h:mm:ss tt"
var resultDate = DateTime.ParseExact(date, "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
here system default format is using to display the date.
if you want to display both as same
Console.WriteLine(date);
Console.WriteLine(resultDate.ToString("dd/MM/yyyy h:mm:ss tt"));

Convert.ToDateTime('Datestring') to required dd-MM-yyyy format of date

I have string of Date which can be in any format of date but I wanted to convert it to dd-MM-yyyy format.
I have tried every Convert.ToDatetime option which converts only to the System format. I want it to convert dd-MM-yyyy format.
Please reply. Thanks in Advance.
Try this
DateTime dateTime = new DateTime();
dateTime = Convert.ToDateTime(DateTime.ParseExact("YouDateString", "dd-MM-yyyy", CultureInfo.InvariantCulture));
This will return DateTime with your Format
Despite there being many answers and this being a duplicate answer, here are some possible solutions:
string formatted = date.ToString("dd-MM-yyyy");
or
string formatted = date.ToString("dd MM yyyy");
This link might help you with more formats and options.
DateTime.ParseExact has an overloaded method to accepts multiple formats, include (all)possible formats you receive and parse the string. Once you get the valid DateTime you could apply desired format in converting to string.
// ex...
string dateString = ...; // your date.
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
var date = DateTime.ParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None);
//convert to desired format.
var strDate = date.ToString("dd-MM-yyyy");
Here we go:
DateTime time = DateTime.Now;
Console.WriteLine(time.Day + "/" + time.Month + "/" + time.Year);
//return: 22/05/2016

"String was not recognized as a valid DateTime in Asp.net

string JDate = "21/01/2016 3:47 PM";
when I convert jDate to DateTime, An error Occurred
DateTime journeyDate = DateTime.ParseExact(JDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
String was not recognized as a valid DateTime.
You need to add TT for the PM.
You should add tt form PM as #SLaks said to convert the string to DateTime
string JDate = "21/01/2016 3:47 PM";
DateTime journeyDate = DateTime.ParseExact(JDate, "dd/MM/yyyy h:mm tt", CultureInfo.InvariantCulture);
Adding tt will fix the issue.
"dd/MM/yyyy h:mm tt"

How to convert this date time string?

I am getting this date time string from Sharepoint:
2013-01-01 08:00:00:000
I want to convert it to
2013-01-01 08:00:00 AM
Any ideas how to do that? Every time I try to use Date.Parse, I get an error saying the string is not a valid date time type
Best option is parsing it to DateTime and get it's string representation with a specific format.
string s = "2013-01-01 08:00:00:000";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// 2013-01-01 08:00:00 AM
}
Based on Jon's comment, you can use ParseExact as well.
DateTime dt = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture);
string s = dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
Or you can format directly that ParseExact return value;
string s = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);

Categories