getting issue with conversion from string to DateTime - c#

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.

Related

Razor-pages ParseExact string

I am trying to format this string into a "MM-dd-yyyy" I am not sure what I am doing wrong I know its a string that I need to get ParseExact into a date.
20210921124857177 is the value and the error I am getting is not in an acceptable format
#{var contentLastModified = #item.GetValue("LastModified").ToString();
DateTime dateTimeLastModified = DateTime.ParseExact(contentLastModified, "MM-dd-yyyy",CultureInfo.InvariantCulture);
}
<div>LastModified : #dateTimeLastModified</div>
The value 20210921124857177 does not match the format MM-dd-yyyy.
At a guess, the format you need would be yyyyMMddHHmmssfff:
DateTime dateTimeLastModified = DateTime.ParseExact(contentLastModified, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
/* Result: 2021-09-21 12:48:57 */

Parse String date to specific format

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.

Convert a string to datetime format in asp .net

I have a date time string "12-24-2013 15:19:29" which is in "MM-dd-yyyy HH:mm:ss". I want to convert this string to datetime. But the format should not change.ie, it should be the same "MM-dd-yyyy HH:mm:ss" format.
When I used following method,
DateTime dt2 = DateTime.ParseExact(date31strin, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
the format is changed to "dd-MM-yyyy HH:mm:ss". I have tried some other method also, but still getting this same format.
First, you should be parsing the string to a DateTime and then format it to the second format using ToString() method.
//Convert your string to a DateTime
DateTime dt2 = DateTime.ParseExact(date31strin,
"MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
//If you want to change the format
string secondFormat = dt2.ToString("Second format string");
Note: Date is a Date and it does not have a format. If you need to convert the string to a DateTime, first line of code is enough
You are not changing the format.
You are parsing a string into a DateTime object which does not have a format.
When you decide to present the DateTime, you can format it any way you wish.
to parse String to DateTime use method DateTime.TryParse (http://msdn.microsoft.com/cs-cz/library/ch92fbc1%28v=vs.110%29.aspx) and then use DateTime formating (http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx) to output to aspx page
//frndzz if you are using j query and you don't convert date in(MM/dd/yyyy) to //(dd/MM/yyyy) then this code will help you
// i try it and i get the answer
string sd=txtmydate.Text //sd=04/27/2015 (MM-dd-yyyy) format
string [] sdate = sd.Split('-');
string fd = sdate[1];
fd=string.Concat(sdate[1],"-",sdate[0],"-",sdate[2]);
DateTime dt = Convert.ToDateTime(fd);
txtshow.Text = dt.ToString("dd/MM/yyyy");
//txtshow will show you date as 27-04-2015 (dd/MM/yyyy) format
thanks
How to convert string to DateTime:
string iDate = "Absent";
aEmployeeAttendence.Intime = DateTime.Parse(iDate);
//aEmployeeAttendence (object)
//Intime (field)

Convert string to datetime fails

I can't seem to find how to convert a string to a datetime. Here is what my date looks like: "23-nov-12".
I tried the following:
DateTime convertedDate = DateTime.ParseExact("dd-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);
and also
DateTime convertedDate = DateTime.ParseExact("0:d-MMM-yy", "23-nov-12", CultureInfo.InvariantCulture);
But I always get the following error:
String was not recognized as a valid DateTime.
Your arguments are in the wrong order. The date goes first.
DateTime convertedDate = DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture);
See: DateTime.ParseExact()
You got the parameters backwards.
The input string goes first.
DateTime.ParseExact("23-nov-12", "dd-MMM-yy", CultureInfo.InvariantCulture)
This code Works...
string dt = "23-nov-12";
Console.WriteLine(Convert.ToDateTime(dt));
It produces 11/23/2012 12:00:00 AM as output
Try It!

DateTime.Parse with a custom DateTimeFormatInfo throws an exception

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

Categories