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
Related
DateTime sStartDate = DateTime.Parse(Convert.ToString(ViewState["StartDate"]));
string sEndDate1 = Convert.ToString(ViewState["EndDate"]);
DateTime sEndDate = DateTime.ParseExact(sEndDate1, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
After DateTime sEndDate its shows exception : string was not recognized as a valid datetime
Change the format to dd/MM/yyyy HH:mm:ss:
DateTime.ParseExact(sEndDate1, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
The hh format represents the hour as a number from 01 through 12.
If sEndDate1 hour is ie 13:00:00 it will throw an exception because it is out of range.
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"
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
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);
In our application server is giving date time string in format like this
"2013-01-14 T 06:33 PST" and "2013-01-14 T 06:33 PDT" or "2013-01-14 T 06:33 PLT"
How could I parse those strings and get DateTime in C#?
Try this one:-
DateTime dt1 = DateTime.ParseExact("2013-01-14 21:09:06 PST".Replace("PST", "+2"), "yyyy-mm-dd HH:mm z", culture);
DateTime dt2 = DateTime.ParseExact("2013-01-14 21:09:06 PDT".Replace("PDT", "+2"), "yyyy-mm-dd HH:mm z", culture);
DateTime dt3 = DateTime.ParseExact("2013-01-14 21:09:06 PLT".Replace("PLT", "+2"), "yyyy-mm-dd HH:mm z", culture);
Once you get the date time you can make conversions using TimeZoneInfo
DateTime dateTime = DateTime.Parse("2013-01-14 T 06:33");
TimeZoneInfo PST = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
TimeZoneInfo yourZone = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time"); //For example
DateTime yourLocalTime = TimeZoneInfo.ConvertTime(dateTime, PST , yourZone );