Using ViewState string was not recognized as a valid datetime - c#

DateTime sStartDate = DateTime.Parse(Convert.ToString(ViewState["StartDate"]));
string sEndDate1 = Convert.ToString(ViewState["EndDate"]);
DateTime sEndDate = DateTime.ParseExact(sEndDate1, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
After DateTime sEndDate its shows exception : string was not recognized as a valid datetime

Change the format to dd/MM/yyyy HH:mm:ss:
DateTime.ParseExact(sEndDate1, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
The hh format represents the hour as a number from 01 through 12.
If sEndDate1 hour is ie 13:00:00 it will throw an exception because it is out of range.

Related

Not recognized valid date date time

Need to convert 08-13-2022 10:27 PM to 08/12/2022.
This is my code:
DateTime date = DateTime.Parse(String.Format("MM/dd/yyyy", row["POSTING_DATE"]));
row["POSTING_DATE"] = Convert.ToDateTime(date);
But I get this error:
not recognized valid date date time
You can use the DateTime.ParseExact method as follows:
string POSTING_DATE = "08-13-2022 10:27 PM";
DateTime date = DateTime.ParseExact(POSTING_DATE, "MM-dd-yyyy hh:mm tt", System.Globalization.CultureInfo.InvariantCulture);
POSTING_DATE = date.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(POSTING_DATE);//prints 08/13/2022

parse string to datetime using parse exact

I am trying to parse a string to datetime using ParseExact but I keep failing..
I tried below but received an error: String was not recognized as a valid DateTime.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = new DateTime();
d = DateTime.ParseExact(topA, "dd/MM/yyyy HH:mm:ss tt", null);
Based on your string, right format should be M/dd/yyyy hh:mm:ss tt with preferable InvariantCulture.
string topA = "3/25/2016 12:00:00 AM";
DateTime d = DateTime.ParseExact(topA, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture);

How to convert this date time string?

I am getting this date time string from Sharepoint:
2013-01-01 08:00:00:000
I want to convert it to
2013-01-01 08:00:00 AM
Any ideas how to do that? Every time I try to use Date.Parse, I get an error saying the string is not a valid date time type
Best option is parsing it to DateTime and get it's string representation with a specific format.
string s = "2013-01-01 08:00:00:000";
DateTime dt;
if(DateTime.TryParseExact(s, "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture).Dump();
// 2013-01-01 08:00:00 AM
}
Based on Jon's comment, you can use ParseExact as well.
DateTime dt = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture);
string s = dt.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);
Or you can format directly that ParseExact return value;
string s = DateTime.ParseExact("2013-01-01 08:00:00:000",
"yyyy-MM-dd HH:mm:ss:fff",
CultureInfo.InvariantCulture)
.ToString("yyyy-MM-dd HH:mm:ss tt", CultureInfo.InvariantCulture);

Convert custom date to mysql datetime

I have a custom date format that I want to convert to Datetime so I can then insert into my database, I tried using Datetime.ParseExact() But I think I'm misunderstanding something as the code throws a System.FormatException.
I have the following date format from a csv
> 6/11/2014 9:00
and I wish to convert it to the mysql datetime format
> 0000-00-00 00:00:00 OR yyyy-MM-dd HH:mm:ss
Notice they haven't included the seconds in the original date so I am unsure (without appending them to the end) how to set all records to just have "00" for seconds as it is not available.
I tried the following which throws an exception
DateTime myDate = DateTime.ParseExact("6/11/2014 9:00", "yyyy-MM-dd HH:mm",
System.Globalization.CultureInfo.InvariantCulture);
first thing you need to convert string to date time and than convert datetime tos tring
string strd = "6/11/2014 9:00";
DateTime dt ;
//convert datetime string to datetime
if(DateTime.TryParse(strd, out dt))
{
//convert datetime to custom datetime format
Console.WriteLine("The current date and time: {0: yyyy-MM-dd HH:mm:ss}",
dt); ;
}
output
I know this is late to answer that but I'm really surprised none of answer consider to use IFormatProvider to prevent a possible parsing error because of / format specifier or considering your string is a standard date and time format for your CurrentCulture or not so you can or can't use DateTime.TryParse(string, out DateTime) overload directly.
First of all, let's look at what DateTime.ParseExact documentation says:
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 or an exception is thrown.
In your case, they don't match. You should use d/MM/yyyy H:mm format to parse your example string with a culture that have / as a DateSeparator. I almost always suggest to use DateTime.TryParseExact method in this kind of situations;
string s = "6/11/2014 9:00";
DateTime dt;
if(DateTime.TryParseExact(s, "d/MM/yyyy H:mm", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
// result will be 2014-11-06 09:00:00
}
If you know formats of your dates, then you can do this:
string stringDate = "6/11/2014 9:00";
//Your date formats of input
string[] dateFormats = new string[]
{
"d/MM/yyyy H:mm",
"dd/MM/yyyy H:mm",
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy H:mm:ss",
"dd/MM/yyyy HH:mm:ss"
/* And other formats */
};
DateTime convertedDate;
bool isSuccessful = DateTime.TryParseExact(stringDate, dateFormats,
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out convertedDate);
if (isSuccessful)
{
//If conversion was successful then you can print your date at any format you like
//because you have your date as DateTime object
Console.WriteLine(convertedDate.ToString("dd-MM-yyyy HH:mm:ss")); /* Or other format you want to print */
}
I hope it will be helpful to you.

DateTime conversion exception

This is code
String date = "1980/1/1";
DateTime dateTime = DateTime.ParseExact(date, "yyyy/MM/DD", null);
// I have also tried
// DateTime dateTime = DateTime.ParseExact(date, "yyyy/MM/DD", CultureInfo.InvariantCulture);
and this is Exception
String was not recognized as a valid DateTime.
Update
Getting same error using following code
DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/D", null);
use single M and Single d
DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/d", null);
Single M will take care for month 01, 1 to 12, similarly Single d will take care of day from 1 to 31, including 01 to 09
You may see: Custom Date and Time Format Strings - MSDN
try this (tested)
String date = "1980/1/1";
DateTime dateTime = DateTime.ParseExact(date, "yyyy'/'M'/'d",null);
the character slash is between single qoutation.
use only yyyy/M/D. it threw an exception because it is expecting yyyy/01/01 two digits for month and day.
DateTime dateTime = DateTime.ParseExact(date, "yyyy/M/d", null);

Categories