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 );
Related
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
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"));
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.
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);
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);