Why is this code throwing an exception?
var dateTime = "2012-03-21_15.12";
var format = new DateTimeFormatInfo()
{
FullDateTimePattern = "yyyy-MM-dd-HH_mm.ss"
};
// FormatException: String was not recognized as a valid DateTime.
var parse = DateTime.Parse(dateTime, format);
Your format string and the date string do not match.
You seem to have forgotten either the hours or minutes portion in the date string.
This:
var dateTime = "2012-03-21_15.12";
Should probably look like:
var dateTime = "2012-03-21-15_54.12";
And I suggest using DateTime.ParseExact:
DateTime.ParseExact("2012-03-21-16_15.12",
"yyyy-MM-dd-HH_mm.ss",
CultureInfo.InvariantCulture,
DateTimeStyles.None)
You may want to use DateTime.ParseExact as this will take a datetime format pattern as a parameter.
DateTime.ParseExact
Related
My system time is of the format dd-MMM-yy (02-Dec-16). The format I want to convert it to is "yyyy/MM/dd". I've basically been playing around with all the other datetime formats that my system offers and this is the parsing statement I've figured out that works for All of them (except this) -
CultureInfo provider = CultureInfo.InvariantCulture;
string date_format = "yyyy/MM/dd HH:mm:ss tt";
DateTime now_value = DateTime.ParseExact(DateTime.Now.ToString(date_format), date_format, provider);
return now_value.ToString(date_format);
But this doesn't work for the aforementioned dd-MMM-yy format. Can someone please tell me what I am doing wrong here?
(Sidebar -Is there a more efficient way in which I can write this above snippet?)
You don't need to convert DateTime to string and then convert back to DateTime and again back to string, if you have DateTime input just call the ToString with the format as below
string dt =DateTime.Now.ToString("yyyy/MMM/dd", CultureInfo.InvariantCulture);
for your example :
DateTime now_value = DateTime.ParseExact("02-Dec-16", "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
return now_value.ToString("yyyy/MM/dd");
Try This:
string date_format = "yyyy-MMM-dd";
string date_now = DateTime.Now.ToString(date_format,CultureInfo.CreateSpecificCulture("en-US"));
return date_now;
Even This should also work:
string date_format = "yyyy-MMM-dd";
string date_now = DateTime.Now.ToString(date_format);
return date_now;
I think best way would be to create an extension method for multiple date formats,
var inputDate = "02-Dec-2016";
string[] availaible_input_date_format = { "dd-MMM-yyyy", "dd/MMM/yyyy" }; // add as many formats availible
var date_format = "yyyy/MMM/dd";
DateTime outputDate;
DateTime.TryParseExact(inputDate, availaible_input_date_format, null, DateTimeStyles.None, out outputDate);
Console.WriteLine(outputDate.ToString(date_format));
You can try this:
datetime yourdatetime = new datetime();
string converteddatetime = yourdatetime.toString("yyyy/MM/dd");
This is a String date:
dob = reader.GetValue(7).ToString();` return like "12/2/2012"
But I want to convert (before passing it) to this format "2012212", I have tried
string newDate = dob.ToString("yyyMMdd")
But I got the following error:
The best overloaded method match for
'string.ToString(System.IFormatProvider)has some invalid arguments
any idea ?
Not Exact way of getting output but it will work.
string dob = "12/2/2012";
string[] d1 = dob.Split('/');
string s = d1[2] + d1[1] + d1[0];
You could use SqlDataReader.GetDateTime if underlying return type is DateTime
Then you just need..
reader.GetDateTime(7).ToString("yyyyMdd");
In case, if it is stored and received as a string then I would suggest converting to DateTime first and then look for specific format.
var date = DateTime.ParseExact(dob, "dd/M/yyyy",CultureInfo.InvariantCulture);
var formattedDate = date.ToString("yyyyMdd");
DateTime.ParseExact(reader.GetValue(7), "dd/M/dd", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyyMMdd");
If you have a date in a String with the format "ddMMyyyy" and want to convert it to "yyyyMMdd" you could do like this:
DateTime dt = DateTime.ParseExact(dob, "ddMMyyyy",
CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
You can try below method to convert string to date.
IFormatProvider culture = new CultureInfo("en-US", true);
dob = DateTime.ParseExact(reader.GetValue(7).ToString(), "dd/MM/yyyy", culture).ToString("yyyyMMdd");
Thank you.
I am getting issue while converting string to DateTime.
The value I am receiving as "08-26-2015 10:14:57.898Z".
I am trying to convert the above string to DateTime.
My Code:
DateTime.ParseExact(element.Value,"MM/dd/yyyy HH:mm:ss",CultureInfo.CurrentCulture);
Exception:
String was not recognized as a valid DateTime.
You have string with different format than you trying for conversion.
Try this
var input = "08-26-2015 10:14:57.898Z";
var date = DateTime.ParseExact(input, "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
You can use:
DateTime dt = DateTime.ParseExact("08-26-2015 10:14:57.898Z", "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
If you use CultureInfo.CurrentCulture(or null) the slash / has a special meaning. It is replaced with the current culture's date separator. Since that is not - but / in US you get an exception. Read
Have you tried Convert.ToDateTime ?
I just tried with your string and it works fine :
var s = "08-26-2015 10:14:57.898Z";
var date = Convert.ToDateTime(s);
String s = "08-26-2015 10:14:57.898Z";
DateTime date;
DateTime.TryParse (s, out date);
Now date variable contains DateTime value you need.
I need to parse date in the following format.
mm_dd_yyyy
I know I can do like this
var dateString = "20050802";
var date = myDate = DateTime.ParseExact(dateString,
"yyyyMMdd",
System.Globalization.CultureInfo.InvariantCulture);
and then replace the -with underscore character.
But is there any other way around to do the same?
So, what's the problem?
Just make appropriate format string: MM_dd_yyyy.
var dateTime = DateTime.ParseExact("08_02_2005", "MM_dd_yyyy", CultureInfo.InvariantCulture); // from string to DateTime
var s = dateTime.ToString("MM_dd_yyyy"); // from DateTime to string
I have a date string with the format "dd.MM.yyyy" (eg. "01.02.2004") and want to convert it to "yyyy-MM-dd" with String.Format(...) or DateTime.ParseExact(...) or whatelse.
Actually I only get exceptions. So the usage of the following code doesn't work:
String dateString = "01.02.2004";
datestring = String.Format("{yyyy-MM-dd}", dateString);
What is wrong with that? Is there an alternative with DateTime.ParseExact(..)?
You need to parse it into a DateTime, then call ToString() with a different format:
DateTime date = DateTime.ParseExact(dateString, "dd.MM.yyyy", CultureInfo.InvariantCulture);
date.ToString("yyyy-MM-dd")
You need to create a Datetime object first.
try something like:
DateTime t = DateTime.parse("01.02.2004");
String result = t.ToString("{yyyy-MM-dd}", CultureInfo.InvariantCulture);
You have to convert it to a DateTime first with an appropriate CultureInfo:
String dateString = "01.02.2004";
var deCulture = System.Globalization.CultureInfo.CreateSpecificCulture("de-DE");
DateTime dt = DateTime.Parse(dateString, deCulture);
dateString = dt.ToString("yyyy-MM-dd");
Demo
String dateString = "01.02.2004";
dateString = DateTime.ParseExact(dateString, "dd.MM.yyyy", CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd");
What's wrong with your attempt is:
When using a format string, the items need to be listed in a format like {0:yyyy-MM-dd}. The 0: is necessary.
When you're using string.Format(), the variable is a string, so the format string yyyy-MM-dd has no meaning there.
You're parsing a string as argument to String.Format? What do you expect out of it?
You should do something like this:
try
{
var date = DateTme.ParseExact("yyyy.MM.dd", dateString, CultureInfo.InvariantCulture);
var result = date.ToString("yyyy-MM-dd");
}
catch(Exception e) { /* ... */}