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);
Related
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"));
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 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 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 have a custom date format that I want to convert to Datetime so I can then insert into my database, I tried using Datetime.ParseExact() But I think I'm misunderstanding something as the code throws a System.FormatException.
I have the following date format from a csv
> 6/11/2014 9:00
and I wish to convert it to the mysql datetime format
> 0000-00-00 00:00:00 OR yyyy-MM-dd HH:mm:ss
Notice they haven't included the seconds in the original date so I am unsure (without appending them to the end) how to set all records to just have "00" for seconds as it is not available.
I tried the following which throws an exception
DateTime myDate = DateTime.ParseExact("6/11/2014 9:00", "yyyy-MM-dd HH:mm",
System.Globalization.CultureInfo.InvariantCulture);
first thing you need to convert string to date time and than convert datetime tos tring
string strd = "6/11/2014 9:00";
DateTime dt ;
//convert datetime string to datetime
if(DateTime.TryParse(strd, out dt))
{
//convert datetime to custom datetime format
Console.WriteLine("The current date and time: {0: yyyy-MM-dd HH:mm:ss}",
dt); ;
}
output
I know this is late to answer that but I'm really surprised none of answer consider to use IFormatProvider to prevent a possible parsing error because of / format specifier or considering your string is a standard date and time format for your CurrentCulture or not so you can or can't use DateTime.TryParse(string, out DateTime) overload directly.
First of all, let's look at what DateTime.ParseExact documentation says:
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
In your case, they don't match. You should use d/MM/yyyy H:mm format to parse your example string with a culture that have / as a DateSeparator. I almost always suggest to use DateTime.TryParseExact method in this kind of situations;
string s = "6/11/2014 9:00";
DateTime dt;
if(DateTime.TryParseExact(s, "d/MM/yyyy H:mm", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
// result will be 2014-11-06 09:00:00
}
If you know formats of your dates, then you can do this:
string stringDate = "6/11/2014 9:00";
//Your date formats of input
string[] dateFormats = new string[]
{
"d/MM/yyyy H:mm",
"dd/MM/yyyy H:mm",
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy H:mm:ss",
"dd/MM/yyyy HH:mm:ss"
/* And other formats */
};
DateTime convertedDate;
bool isSuccessful = DateTime.TryParseExact(stringDate, dateFormats,
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out convertedDate);
if (isSuccessful)
{
//If conversion was successful then you can print your date at any format you like
//because you have your date as DateTime object
Console.WriteLine(convertedDate.ToString("dd-MM-yyyy HH:mm:ss")); /* Or other format you want to print */
}
I hope it will be helpful to you.