Convert a string has datetime to DateTime - c#

I want to convert a string of date and time to DateTime structure, but it is giving this error :
String was not recognized as a valid DateTime
DateTime dt = Convert.ToDateTime("5/15/2018 11:54:18 AM");
string date= dt.ToString("HH:mm");
I'm reading this question but I can't solve this code. What is my mistake?
What is the difference between Convert.ToDateTimeand DateTime.ParseExact() in C#?

Based on all the comments, here's how your code should look like
DateTime dt = DateTime.ParseExact("05/15/2018 11:54:18 AM", "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
string date = dt.ToString("HH:mm");

What is my mistake?
You Mistake is You are Providing argument Convert.ToDateTime() in a wrong Format.
try Providing "DD/MM/YYYY HH:MM:SS" according to Your system date Time Format .Else you need to use TryParseExatct with Specifying Format

Related

How can I convert this date "03/01/2018 12:00 AM" to "2018-03-01" in C#?

I am using this code to convert a date 03/01/2018 12:00 AM to 2018-03-01 in C#:
DateTime startDate = DateTime.ParseExact(TextBox1.Text.ToString(),
"yyyy-mm-dd",
System.Globalization.CultureInfo.InvariantCulture);
but it throws an exception
String was not recognized as valid datetime
This Will Work Like A Charm
string bs = "03/01/2018 12:00 AM";
String startDate = DateTime.ParseExact(bs,"MM/dd/yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
Console.WriteLine(startDate);
// Outputs 2018-03-01
You can verify the code here
A DateTime represents a particular point in time. ParseExact is a way of turning a string into a DateTime. By saying ParseExact with "yyyy-mm-dd" you are telling it that the string you are giving it begins with a four digit year, which it doesn't. Fix the format string that you are supplying so that the parse works.
Once you have the value in your DateTime variable, you can use ToString("yyyy-mm-dd") to turn that DateTime back into a string.
Remove AM from your Textbox and edit format string, then your sample code will work.
This line run successfully :
DateTime startDate = DateTime.ParseExact("03/01/2018 12:00", "MM/dd/yyyy hh:mm", System.Globalization.CultureInfo.InvariantCulture);
string inputDate = "03/01/2018 12:00 AM";
string outputDate = DateTime.Parse(inputDate).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

How to convert a date string from "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt"?

I need to compare two date format strings:
dateString in "dd-MMM-yy" format
with
referenceDateString in "M/dd/yyyy hh:mm:ss tt" format respectively.
For that, I need to convert the dateString = "dd-MMM-yy" to "M/dd/yyyy hh:mm:ss tt".
However, Got an error while trying to do that:
"Error: string was not recognized as a valid datetime".
The C# code I used given below.
string dateString = "19-Dec-14";
string AsofDate = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Edit 1:
In the actual code the dateString obtaining after reading a csv file which is supplied as "19-Dec-14", that's why it's in the string format.
Please help, am pretty new to C#. Thanks.
Habib already gave the answer on his comments, I try to add it as an answer;
From DateTime.ParseExact(String, String, IFormatProvider)
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
In your case, clearly they don't. First, you need to parse your string to DateTime with proper format (which is dd-MMM-yy with an english-based culture), then you can get the string represention of your DateTime with specific format.
string s = "19-Dec-14";
DateTime dt;
if(DateTime.TryParseExact(s, "dd-MMM-yy", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// Result will be 12/19/2014 12:00:00 AM
}
It's not entirely clear what you are trying to do, but in order to parse that date you have on the first line, you would use something like this:
DateTime AsofDate = DateTime.ParseExact(dateString, "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture);
Note a couple things here: I've changed the data type of AsofDate from string to DateTime because that's what DateTime.ParseExact returns. Also, I've modified the custom format string to match the format of the string you are trying to parse as a date ("19-Dec-14").

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)

date conversion error

I am developing windows application.
In that i have date in the string format as>> fileDate="15/03/2013"
I want it to be get converted into date format as my database field is datetime.
I used following things for it>>
DateTime dt = DateTime.ParseExact(fileDate, "yyyyy-DD-MM", CultureInfo.InvariantCulture);
DateTime dt = DateTime.Parse(fileDate);
Both of these methods proved failure giving me error>>
String was not recognized as a valid DateTime.
What can be mistake?
Is there another technique to do that?
string fileDate = "15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);
You have to give the date format according to the date string you have to ParseExact. You can see more on Custom DateTime format - MSDN
Change
"yyyy-MM-dd HH:ss"
To
"dd/MM/yyyy"
Your code would be
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should do this:
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy",CultureInfo.InvariantCulture);
You must pass in the string for the format ("dd/MM/yyyy") in the same style that you pass in the string fileDate.
u may try with this
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = dateFormat.parse("ur_dateString")
In your current code you are using format "yyyyy-DD-MM" which is wrong since date part require lower case d not upper case D. , Also for year part you are specifying 5 ys, it should be 4, like yyyy, the order according to your date string should be: "dd/MM/yyyy". To be on the safe side you can even use "d/M/yyyy", which would work for single digit or double digit day/month.
So your code should be:
string fileDate="15/03/2013";
DateTime dt = DateTime.ParseExact(fileDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can see more on Custom DateTime format - MSDN
It's because string "15/03/2013" cannot really be parsed as DateTime with format string "yyyy-MM-dd HH:ss".

Problem with date type

i receive this date: 9/20/2010 3:32:32 PM
i need to convert to datetime.
i try:
DateTime DateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "dd/M/yyyy", CultureInfo.InvariantCulture);
but i get error: String was not recognized as a valid DateTime.
in my computer the region is: Hebrew (Israel) dd/MM/yyyy for short date and hh:mm for short time
how to fix it ?
thank's in advance
If you're receiving "9/20/2010 3:32:32 PM" as a string, then trying to parse it as if it were in the "dd/MM/yyyy" format is clearly wrong - that's try to use a month of 20. You're also only parsing part of the string - you need to either trim your string or provide the complete format.
Try this:
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM",
"M/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
Note that using this sort of strict parsing will only work if you can guarantee that that will always be the format. Where are you getting this data from?
How could it work man.You are converting into "dd/MM/yyyy " & putting month as 20.In your
question dd/M/yyyy is wrong.It will like dd/MM/yyyy.
By default format is MM/DD/yyyy.
simple way to do .......
DateTime DateFrom = DateTime.Parse("9/20/2010 3:32:32 PM");
if you want to provide a specific Format so use like that
DateTime DateFrom = DateTime.ParseExact("20/09/2010 3:32:32 PM", "dd/MM/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
I hope it works.
It looks like your original date string is in a US format (i.e. m/dd/yyyy). Try replacing your third parameter with new CultureInfo("en-US")
DateTime dateFrom = DateTime.ParseExact("9/20/2010 3:32:32 PM", "M/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
Works for me
I wouldn't use ParseExact() when I know that time string is formatted by invariant culture.
DateTime dateFrom = DateTime.Parse(dateString, CultureInfo.InvariantCulture);
is both more compact and more clear.

Categories