DateTime conversion exception - c#

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);

Related

Error: String was not recognised as a valid Datetime format [duplicate]

I have a string like this:
250920111414
I want to create a DateTime object from that string. As of now, I use substring and do it like this:
string date = 250920111414;
int year = Convert.ToInt32(date.Substring(4, 4));
int month = Convert.ToInt32(date.Substring(2, 2));
...
DateTime dt = new DateTime(year, month, day ...);
Is it possible to use string format, to do the same, without substring?
Absolutely. Guessing the format from your string, you can use ParseExact
string format = "ddMMyyyyHHmm";
DateTime dt = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
or TryParseExact:
DateTime dt;
bool success = DateTime.TryParseExact(value, format,
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
The latter call will simply return false on parse failure, instead of throwing an exception - if you may have bad data which shouldn't cause the overall task to fail (e.g. it's user input, and you just want to prompt them) then this is a better call to use.
EDIT: For more details about the format string details, see "Custom Date and Time Format Strings" in MSDN.
You could use:
DateTime dt = DateTime.ParseExact(
date,
"ddMMyyyyHHmm",
CultureInfo.InvariantCulture);
string iDate = "05/05/2005";
DateTime oDate = Convert.ToDateTime(iDate);
DateTime oDate = DateTime.ParseExact(iString, "yyyy-MM-dd HH:mm tt",null);
DateTime Formats

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);

Parse string to DateType ASP.NET C# exception - The string was not recognized as a valid DateTime

So I have a date which is in this format. My goal is to add 7 days to this string startdate and post it into a database as a string. However, I have to convert it to datetime to allow me to add days to it. I am reading startdate from a database but this is what it looks like.
string startdate = "10-03-2018 03:15PM";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime starttime2 = DateTime.ParseExact(startdate, "MM/dd/yyyy HH:mm tt", culture);
// It is breaking on the above line with the error - The string was not recognized as a valid DateTime.
DateTime endtime2 = starttime2.AddDays(+7);
Anyone able to help me solve this issue? I am new to C# and would appreciate any help at all..
Thank you
You have specified wrong format actually. You should be specifying the following format:
"dd-MM-yyyy hh:mmtt"
as your date is in format :
"10-03-2018 03:15PM"
Assuming that the first number us for day and second is for month, otherwise you can swap those.
You can see more details on the usage of ParseExact here.
Try this:
string startdate = "10-03-2018 03:15PM";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime starttime2 = DateTime.ParseExact(startdate, "dd-MM-yyyy hh:mmtt", culture);
no space between mm and tt. also this is 12 hours format so hh
I think this will help you
public static DateTime AddDaysToMyDate(string date)
{
return DateTime.ParseExact(date, "dd-MM-yyyy hh:mmtt", new CultureInfo("en-US", true)).AddDays(7);
}
Use it as
DateTime newDateTime = AddDaysToMyDate("10-03-2018 03:15PM");

Using ViewState string was not recognized as a valid datetime

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.

String to DateTime conversion as per specified format

I would like to have my end result in date format as per the specified format i.e YYMMDD how can i get this from a string given as below
string s="110326";
From string to date:
DateTime d = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
Or the other way around:
string s = d.ToString("yyMMdd");
Also see this article: Custom Date and Time Format Strings
DateTime dateTime = DateTime.ParseExact(s, "yyMMdd", CultureInfo.InvariantCulture);
although id recommend
DateTime dateTime;
if (DateTime.TryParseExact(s, "yyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dateTime))
{
// Process
}
else
{
// Handle Invalid Date
}
To convert DateTime to some format, you could do,
string str = DateTime.Now.ToString("yyMMdd");
To convert string Date in some format to DateTime object, you could do
DateTime dt = DateTime.ParseExact(str, "yyMMdd", null); //Let str="110719"

Categories