I have the following output in string:
24/05/15 11:40:50 AM
now I want to convert this string into -> 2015-05-24 11:40:50.000
I have tried below method but it gives me error :
String was not recognized as a valid DateTime.
DateTime.ParseExact("24/05/15 11:40:50 AM",
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture);
From documentation;
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.
In your case, they are not.
First, you can parse it to DateTime with specific format and you can generate a string representation with a specific format from that DateTime. Like;
string s = "24/05/15 11:40:50 AM";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yy hh:mm:ss tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss.fff"));
}
Prints;
2015-05-24 11:40:50.000
Related
I have string format "20151210T11:25:11123", can't convert to type DateTime in C# help me?
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
You are using a time of 20151210T11:25:11123 but telling it to parse it as if it were formatted as dd/MM/yyyy hh:mm tt. The format does not match the string, so you get a FormatException. You need to provide a format that matches the string you have. It isn't clear to me what the last 5 digits are but a format like yyyyMMddThh:mm:ssfff will parse the string as 12/10/2015 11:25:11 AM. You may need to adjust the last part of the format to match whatever is actually encoded there in your string.
string date = "20151210T11:25:11123";
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM
with value datetime string ="20160121T13:26:24090"
code error :
DateTime datea = DateTime.ParseExact(date, "yyyyMMddThh:mm:ssfff", CultureInfo.InvariantCulture)
Console.WriteLine(datea); // 12/10/2015 11:25:11 AM
I use javascript to pick a date and display the format as (11-05-2015 17:37)
and I try to parse it to date time as the code below
DateTime taskDate = Convert.ToDateTime(txtDate.Text);
and save it into my date base like
TO_DATE('" + createOn + "')
its give me and error call "String was not recognized as a valid DateTime."
anyone have any others method to parse it to stamptime ?
the txtDate.Text value is 27-05-2015 09:37.
Convert.ToDateTime uses your current thread culture format when converting from string to datetime.
If string you converting from has another format, you need to use DateTime.ParseExact and explicitly provide appropriate format.
For example, in you case it should be
DateTime taskDate = DateTime.ParseExact("11-05-2015 17:37", "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture);
Also have a look to custom datetime format strings for your reference.
You can use DateTime.ParseExact or DateTime.TryParseExact
Check below code:
DateTime taskDate = DateTime.ParseExact(txtDate.Text, "dd-MM-yyyy hh:mm", CultureInfo.InvariantCulture);
Or
DateTime taskDate;
if (DateTime.TryParseExact(txtDate.Text, "dd-MM-yyyy hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out taskDate))
{
//code if date valid
}
else
{
//code if date invalid
}
I have a date which comes in a string like so:
09/25/2014 09:18:24
I need it like this (yyyy-mm-dd):
2014-09-25 09:18:24
The object that this date goes into is a nullable date.
Tried this does not work:
DateTime formattedDate;
bool result = DateTime.TryParseExact(modifiedDate, "yyyy-MM-dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out formattedDate);
Any clues?
Thanks in advance.
From DateTime.TryParseExact
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.
In your case, they are not. Use yyyy-MM-dd HH:mm:ss format instead.
string s = "2014-09-25 09:18:24";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
It is a little bit unclear but if your string is 09/25/2014 09:18:24, then you can use MM/dd/yyyy HH:mm:ss format instead. Just a tip, "/" custom format specifier has a special meaning as replace me with current culture or supplied culture date separator. That means, if your CurrentCulture or supplied culture's DateSeparator is not /, your parsing operation will fail even if your format and string matches exactly.
If you have already a DateTime and you want to format it, you can use DateTime.ToString(string) method like;
dt.ToString("yyyy-mm-dd", CultureInfo.InvariantCulture); // 2014-09-25
or
dt.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture); // 2014-09-25 09:18:24
Remember, a DateTime does not have any implicit format. It just contains date and time values. String representations of them have formats.
In answer to your question, to convert it as you prefer, do it like this:
string originalDate = "09/25/2014 09:18:24";
DateTime formattedDate;
if (DateTime.TryParseExact(originalDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out formattedDate))
{
string output = formattedDate.ToString("yyyy-mm-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
And then output will have your desired format.
DateTime dateOf = Convert.ToDateTime("09/25/2014 09:18:24");
string myFormat = "yyyy-mm-dd";
string myDate = dateOf.ToString(myFormat); // output 2014-18-25
Datetime format
I need to know exact date format that will perse the string 16-Aug-78 12:00:00 AM. I have tried various string like "ss-MMM-yy hh:mm:ss". Is there any way for finding it to converting to any general format. I am using CultureInfo.InvariantCulture class.
Your string format is wrong. It has to match your string format exactly. You can use dd-MMM-yy hh:mm:ss tt format instead.
Here an example in LINQPad.
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.ParseExact(s, "dd-MMM-yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
date.Dump();
Custom Date and Time Format Strings
Or, since dd-MMM-yy hh:mm:ss tt is a standart date and time format for InvariantCulture, you can directly DateTime.Parse method like;
string s = "16-Aug-78 12:00:00 AM";
var date = DateTime.Parse(s, CultureInfo.InvariantCulture);
date.Dump();
Here a demonstration.
Is there any way for finding it to converting to any general format?
There is no way to get format of a string except you create your own formatting. Only you can know what is your string format exactly, computer can't.
For example; 01/02/2014 can be 1 February 2014 or 2 January 2014 depends on which custom format you can parse it.
Try as below
var dstring = "16-Aug-78 12:00:00 AM";
DateTime result;
var matchingCulture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(ci => DateTime.TryParse(dstring, ci, DateTimeStyles.None, out result))
You have wrong format string, you are using ss for day it should be dd. This article Custom Date and Time Format Strings explains what you need for custom format.
Use
"dd-MMM-yy hh:mm:ss tt"
Instead of
"ss-MMM-yy hh:mm:ss"
You can use DateTime.ParseExact to parse the string.
DateTime dt = DateTime.ParseExact("16-Aug-78 12:00:00 AM", "dd-MMM-yy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
You can try with DateTime.TryParseExact():
string strDate = "16-Aug-78 12:00:00 AM";
DateTime datDate;
if(DateTime.TryParseExact(strDate , new string[] {"dd-MMM-yy hh:mm:ss tt" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datDate))
{
Console.WriteLine(datDate);
}
else
{
Console.WriteLine("Error in datetime format");
}
I want to convert a string to datetime format.
The thing is that the string comes in different formats.
For instance, in the code below, strDate can be "2/20/2014 1:41:57 PM" or "20/02/2014 13:44:56".
Convert.ToDateTime(strDate) executes well just for one format (the one on the user browser settings) and generates an error for the other.
How can I successfully convert the string to datetime independently of the string format?
Thanks
DateTime dt = Convert.ToDateTime(strDate);
You can use DateTime.TryParseExact or Datetime.ParseExact with multiple formats like:
string dateStr = "20/02/2014 1:41:57 PM";
string[] dateFormats = new[]
{
"d/M/yyyy h:mm:ss tt",
"M/d/yyyy h:mm:ss tt",
};
DateTime dt;
if (DateTime.TryParseExact(dateStr,
dateFormats,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
//valid dates for formats
}
else
{
//invalid date
}
the problem with this approach is that it would give you inconsistent results with strings like 10/02/2014 1:41:57 PM, The above code would parse it as 10th Feb 2014, not as October 2nd 2014, to avoid this you can customize your client side to return date in specific format and then parse accordingly.
you want DateTime.Parse() or DateTime.TryParse()
i.e.
DateTime dt;
if(DateTime.TryParse(stringDate, out dt)
{
//successful datetime conversion
}
if you know the string will be one of several exact formats, you can use DateTime.TryParseExact() and pass in a string array of each format (the second overload).