I am getting error String was not recognized as a valid DateTime while converting string to datetime format. I am trying to convert "25-11-2013 06:25:33 PM" to date format. Do any one can help me to solve this issue.
protected void text_changed(object sender, EventArgs e)
{
if (frm.Text == "")
{
Label1.Visible = true;
Label1.Text = "You can leave from textbox blank";
return;
}
string dt = TextBox1.Text;
string amt = dt.ToString();
string ams = amt.ToString() + " " + frm.Text;
DateTime dts = DateTime.ParseExact(ams, "MM/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
string ams1 = amt.ToString() + "" + TextBox2.Text;
DateTime dts1 = DateTime.ParseExact(ams1, "MM/dd/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
TimeSpan dur = DateTime.Parse(dts1.ToString()).Subtract(DateTime.Parse(dts.ToString()));
double res = 0.0;
res = dur.TotalHours * 20;
TextBox3.Text = res.ToString();
}
If what the user enters is "25-11-2013 06:25:33 PM" then your format string has to be:
string dateFormat = "dd-MM-yyyy hh:mm:ss tt";
The format string you have in your code "MM/dd/yyyy hh:mm:ss tt" requires the user to enter the date as "11/25/2013 06:25:33 PM". All the custom DateTime format string options are described here: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Use a calendar control
You are going to have issues with parsing the dates if you allow users to just enter a free form string. Your best option is to add a calendar control or some other way of structuring the user input so that you don't have to parse a free-form string.
Use the generic converter
Using Convert.ToDateTime will save you a bunch of hassle in setting up the expected formats as it tries it's best to find a date format that will fit. On the other hand you may not get what you expected if the date the user enters can be parsed in multiple ways...
string dts = "09/10/11 06:25";
DateTime dt = System.Convert.ToDateTime(dts);
On my Swedish system it gets converted to 2009-10-11, yy/mm/dd but using a US context the expected date is 2011-09-10, mm/dd/yy.
Use CultureInfo if you can
If you can get the user to indicate or select their culture then parsing date/times from free-form will be much easier - since the user is more likely to enter a culturally correct string.
//this is parsing the datetime using Swedish format
string theCulture = "sv-SE";
string localDts = "2013-11-25 06:25";
DateTime localDt = DateTime.Parse(localDts, System.Globalization.CultureInfo.CreateSpecificCulture(theCulture));
Use an array of format strings
If that isn't possible you should think about the different ways that a user may enter dates and times in your system and add them to an array of supported strings. You may need to add a load of different strings since each format string parses one exact format and the user may enter months,days,hours etc as single digits or omit the seconds part. Here is an example which parses a bunch of different formats, this is in no way complete coverage of all the alternatives...
//A few different strings to test
string dts1 = "25-11-2013 6:25:33";
string dts2 = "11/25/2013 6:25:33";
string dts3 = "25-11-2013 06:25:33";
string dts4 = "11/25/2013 06:25:33";
string dts5 = "25-11-2013 6:25:33 PM";
string dts6 = "11/25/2013 6:25:33 PM";
string dts7 = "25-11-2013 06:25:33 PM";
string dts8 = "11/25/2013 06:25:33 PM";
string dts9 = "25-11-2013 6:5:33 PM";
string dts10 = "11/25/2013 6:5:33 PM";
//The supported datetime formats as an array
string[] dateFormats = {
"dd-MM-yyyy hh:mm:ss tt",
"dd-MM-yyyy h:mm:ss tt",
"dd-MM-yyyy h:m:ss tt",
"dd-MM-yyyy HH:mm:ss",
"dd-MM-yyyy H:mm:ss",
"dd-MM-yyyy H:m:ss",
"MM/dd/yyyy hh:mm:ss tt",
"MM/dd/yyyy h:mm:ss tt",
"MM/dd/yyyy h:m:ss tt",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy H:mm:ss",
"MM/dd/yyyy H:m:ss"
};
//Parse all the sample strings
DateTime dt1 = DateTime.ParseExact(dts1, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt2 = DateTime.ParseExact(dts2, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt3 = DateTime.ParseExact(dts3, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt4 = DateTime.ParseExact(dts4, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt5 = DateTime.ParseExact(dts5, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt6 = DateTime.ParseExact(dts6, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt7 = DateTime.ParseExact(dts7, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt8 = DateTime.ParseExact(dts8, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt9 = DateTime.ParseExact(dts9, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
DateTime dt10 = DateTime.ParseExact(dts10, dateFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
Related
string s = "20100426T000000Z";
DateTime date = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
Tried this getting invalid string format issue. Can somebody help me in this.
This should do it :
string s = "20100426T000000Z";
DateTime theDateTime = DateTime.ParseExact(s, "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
I have a date time format of yyyyMMdd:HHmmss. I receive the datetime from a device and I can confirm that the format is yyyyMMdd:HHmmss.
For example I get datetime as 20180331:162308. I tried to convert to 03/31/2018 04:23 PM format by using below code:
string localDateTime =
Convert.ToString(protocol.GetParameter(Parameter.localmdmdatetimestr_3043));
string formatString = "yyyyMMdd:HHmmss";
protocol.Log(
"QA" + protocol.QActionID + "|DateTime|localDateTime" + localDateTime,
LogType.Error,
LogLevel.NoLogging);
DateTime LocalDt = DateTime.ParseExact(
localDateTime,
formatString,
CultureInfo.InvariantCulture);
But instead get
System.FormatException: String was not recognized as a valid DateTime
error.
As per your requirement
string localDateTime = Convert.ToString("20180331:162308");
string formatString = "yyyyMMdd:HHmmss";
DateTime LocalDt = DateTime.ParseExact(
localDateTime,
formatString, CultureInfo.InvariantCulture);
Console.WriteLine(LocalDt);
var test = LocalDt.ToString("yyyy/MM/dd HH:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine(test);
Console.ReadLine();
Would like to make a conversion to DateTime from the following strings of datetime format:
"3/20/2017 4:03:03 PM"
"03/20/2017 4:03:03 PM"
"3/20/2017 04:03:03 PM"
"03/20/2017 04:03:03 PM"
etc
Been using try DateTime.ParseExact with the format "MM/dd/yyyy hh:mm:ss tt" but Format Exception is caught, Is there a better way to do this?
You can parse dates passing all custom Formats in an array:
string[] strDates = new string[]{"3/20/2017 4:03:03 PM"
,"03/20/2017 4:03:03 PM"
,"3/20/2017 04:03:03 PM"
,"03/20/2017 04:03:03 PM"};
DateTime dt = DateTime.Now;
string[] strFormats = new string[] { "M/dd/yyyy h:mm:ss tt", "dd/MMM/yyyy h:mm:ss tt","MM/dd/yyyy h:mm:ss tt" };
foreach (var item in strDates)
{
foreach (var format in strFormats)
{
dt = DateTime.ParseExact(item, format, CultureInfo.InvariantCulture);
}
}
Or pass the array directly:
dt = DateTime.ParseExact(item, strFormats, CultureInfo.InvariantCulture, DateTimeStyles.None);
Try this, it will work
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "3/20/2017 4:03:03 PM";
format = "M/dd/yyyy h:mm:ss tt";
try {
result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
is it possible to convert a string (date stored as varchar in database) to datetime in c#?
eg. i'm having a table which stores dates as varchar and it stores values as example
01/21/14 11:42:36 PM
i want to get the result in the format yyyy-mm-dd
i tried,
CultureInfo enUS = new CultureInfo("en-US");
DateTime d;
DateTime.TryParseExact("01/21/14 11:42:36 PM", "yyyy-mm-dd", enUS, DateTimeStyles.None, out d);
also tried below steps:
CultureInfo enUS = new CultureInfo("en-US");
DateTime d = Convert.ToDateTime("01/21/14 11:42:36 PM");
string dt = Convert.ToString(d);
DateTime.TryParseExact(dt, "MM/dd/yy hh:mm:ss tt", enUS, DateTimeStyles.None, out d);
var output = d.ToString("yyyy-mm-dd");
getting value 1/1/0001 12:00.. in dt.
what could be the reason? also in which format do i need to pass the date (dt in above case) to DateTime.TryParseExact(..)
The second parameter of your call should be the format you're passing. After the datetime is created you can specify the output format:
CultureInfo enUS = new CultureInfo("en-US");
DateTime d;
DateTime.TryParseExact("01/21/14 11:42:36 PM", "MM/dd/yy hh:mm:ss tt", enUS, DateTimeStyles.None, out d);
var output = d.ToString("yyyy-MM-dd");
String date = "01/21/14 11:42:36 PM";
DateTime dt = Convert.ToDateTime(date);
var output = dt.ToString("yyyy-MM-dd");
In my wpf app, I've written following code for comparing selected date format.
C# code:
class Harvest_Base
{
public static DateTime storeTime(String date)
{
DateTime returnValue = new DateTime();
if (date == "")
return returnValue;
//Time or Date Component Does not Exist
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", "yyyy-mm-dd",
"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",
"h:mm tt","hh:mm tt","HH:mm:ss","H:mm","HH:mm","h:mmtt"};
DateTime result;
if (DateTime.TryParseExact(date, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
returnValue = result;
else
returnValue = DateTime.Today;
return returnValue;
}
The problem here is, when I set breakpoint and check then 'date' object showing mw selected date properly. Format of date is "yyyy-mm-dd". If I select "2013-08-08" then returnValue showing me date as "08-01-2013 00:08:00". It's totally in different format. If we ignore format, then month is wrong. How should I solve this?
If I select "2013-08-08" then returnValue showing me date as "08-01-2013 00:08:00"
Because mm is minutes where MM is months therefore it is converting it to minutes.
this works well
string[] formats= {"yyyy-MM-dd"};
if (DateTime.TryParseExact(date, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
returnValue = result;
else
returnValue = DateTime.Today;