c# tryParseExact not recognized as valid datetime - c#

I am trying to parse a string that I'm trying to parse.
when I use var newDt = Convert.ToDateTime("3/6/2019 12:00:00 AM +00:00");
It gives me the date with a different timezone. I would get a date time of 3/5/2019 4:00AM
I would like it to parse and return the same date time as the string.
I've tried
var newDt = DateTime.ParseExact("3/6/2019 12:00:00 AM +00:00", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);
I get the error
System.FormatException: 'String was not recognized as a valid
DateTime.
How can I parse the date "3/6/2019 12:00:00 AM +00:00" to return a datetime with the same value of 3/6/2019 12AM

You probably have UTC−08:00 time zone (which I assume your Convert.ToDateTime code part returns 3/6/2019 4:00AM not 3/5/2019 4:00AM) in your machine and that's why when you parse it with offset value, you will get 4 hour added value.
If your string contains UTC offset value, it would be better to parse it to DateTimeOffset instead of DateTime.
var newDt = DateTimeOffset.ParseExact("3/6/2019 12:00:00 AM +00:00",
"M/d/yyyy h:mm:ss tt zzz",
CultureInfo.InvariantCulture);
Then you can use it's .DateTime property to get the value you expected.

Related

ParseExact DateTime error

I have a list of DateTime as strings: dd.MM.yyyy H:mm:ss (The time is 24 hours format but the hour is single digit: 6 instead of 06)
14.12.2016 6:20:21
15.12.2016 8:30:00
16.12.2016 12:30:00
17.12.2016 14:33:00
18.12.2016 18:10:00
I am trying to parse exact the string values as a DateTime object like this:
DateTime.ParseExact(dt, "dd.MM.yyyy H:mm:ss", CultureInfo.InvariantCulture) (dt is the string value from the list)
The problem is I get an error saying the string is not a valid DateTime ...
With the current format, the first and second values in the list work fine, when it get's to the third 'boooom' I get the error.
Am I missing something in my format?
Use the overload of DateTime.ParseExact that accepts an array of valid formats:
string[] formats = { "dd.MM.yyyy H:mm:ss", "dd.MM.yyyy HH:mm:ss" };
var result = DateTime.ParseExact(dt, formats, CultureInfo.InvariantCulture, 0);
UPDATE: As others have noted, H should match both one-digit and two-digit hours, so something else is going on. The following code runs successfully on my system (.NET 4.5.2):
string dt = "16.12.2016 12:30:00";
var result = DateTime.ParseExact(dt, "dd.MM.yyyy H:mm:ss", CultureInfo.InvariantCulture);
I had to parse exact the using Hour, Minute, and Second then use the ToString method to convert to a Month, day, and year format
$myStringDate=[Datetime]::ParseExact($item.myDateObject,'MM/dd/yyyy H:mm:ss',$null).ToString('MM/dd/yyyy')

string was not recognized as a valid datetime while Converting Date from Hijri to Gregorian date

I am Trying to Convert Hijri Date into Gregorian Date I was following this article and My Code is as follows :
var cultureInfo = CultureInfo.CreateSpecificCulture("ar-sa");
string date = "19/12/36 12:00:00 ص";
Getting
string was not recognized as a valid datetime
error in below line
DateTime tempDate = DateTime.ParseExact(date, "dd/MM/yyyy", cultureInfo.DateTimeFormat, DateTimeStyles.AllowInnerWhite);
lblDate.Text = tempDate.ToString("dd/MM/yyyy");
I am getting string was not recognized as a valid datetime. Please can somebody tell me whats wrong with this code?
I think I'm on the right way but.. Let's try something at least.
First of all, DateTime values are always in the Gregorian calendar, basically. There's no such thing as "A DateTime in a UmAlQuraCalendar calendar" - which is used by ar-sa culture - you have to use the UmAlQuraCalendar to interpret a DateTime in a particular way.
Second, when you use DateTime.ParseExact for parsing your string, your string and format does match exactly based on culture you use. Since ص
character seems AMDesignator of ar-sa culture, you should provide tt specifier with your time part as well.
string s = "19/12/36 12:00:00 ص";
DateTime dt;
if(DateTime.TryParseExact(s, "dd/MM/yy hh:mm:ss tt", CultureInfo.GetCultureInfo("ar-sa"),
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt);
}
Note: Since TwoDigitYearMax is 1451 of UmAlQuraCalendar calendar, your 36 will be parsed as 1436 with yy format specifier.
This perfectly parse your question but WAIT! What will be the result? Here it is.
02/10/2015 00:00:00
Why? As I said in the top, you have to use the UmAlQuraCalendar to interpret this DateTime instance.
UmAlQuraCalendar ul = new UmAlQuraCalendar();
Console.WriteLine(ul.GetYear(dt)); // 1436
Console.WriteLine(ul.GetMonth(dt)); // 12
Console.WriteLine(ul.GetDayOfMonth(dt)); // 19

String to date conversion in c# as yyyy/mm/dd

I would like convert string content to date format as yyyy/MM/dd HH:mm:ss tt
string date = "2014-11-20 3:21:00 PM";
DateTime date_=System.DateTime.Now;
var result = DateTime.TryParseExact(date, "yyyy-MM-dd HH:mm:ss tt",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out date_);
But it returns result doesn't match requirement also TryParse function return false. If time zone is not defined, it will return expected result.
HH specifier is for 24 hour clock which is 00 to 23.
You need to use h specifier instead which represents 1 to 12 in 12 hour clock.
Also you don't need to initialize your out parameter value. Definition will be enough like;
string date = "2014-11-20 3:21:00 PM";
DateTime date_;
var result = DateTime.TryParseExact(date, "yyyy-MM-dd h:mm:ss tt",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out date_);
The DateTime data type format is always MM/dd/yyyy hh:mm:ss tt (i.e. Date = {11/20/2014 12:00:00 AM}),
If you want to display the value you can change the format by using ToString extention method

Formatting Date and Datetime with SSIS Script Transformation

I have a flat file containing dates like this: 07/07/2003 12:18:20 PM.
The SSIS Transformation Output Column is set to database timestamp [DT_DBTIMESTAMP].
I have the following method:
public string DbDateTime(string input)
{
return DateTime.ParseExact(input, "M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture).ToString();
}
I need an output like this to the database:
2003-07-07 12:18:00.000
However, I keep getting an error:
String was not recognized as a valid DateTime.
The output is set like this:
Row.OuputDateTimeColumn =
Convert.ToDateTime(DbDateTime(Row.InputDateTimeColumn));
I prefer not to use a Derived Column for the conversion.
Problem 1: You are providing invalid custom format for both Month and Date feilds.
you have two fixed digits in your Date String but you are only providing one digit format as below:
"M/d/yyyy h:mm:ss tt"
so you need to Replace this as below:
if your DateString hour format is 00-12
"MM/dd/yyyy hh:mm:ss tt"
if your DateString hour format is 0-12
"MM/dd/yyyy h:mm:ss tt"
Problem 2: You want to return the Date String in custom format of 2003-07-07 12:18:00.000 i assume it is in the format of yyyy-MM-dd h:mm:ss.ffff.
Note: as both Month and Date are same(07) in your example, you need to adjust them accordingly if my assumption is wrong.
so you need to provide the above custom format to the ToString() function in the return statement.
Complete Code:
public string DbDateTime(string input)
{
return DateTime.ParseExact(input, "MM/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy-MM-dd h:mm:ss.fff");
}
Problem 3: while adding the Custom DateFormat to the database you need to first convert it into the DateTime as below:
Row.OuputDateTimeColumn = DateTime.ParseExact(
DbDateTime(Row.InputDateTimeColumn),"yyyy-MM-dd h:mm:ss.fff"
CultureInfo.InvariantCulture);
Your format is wrong. It doesn't match with your input string. Use MM/dd/yyyy h:mm:ss tt format instead. For example;
public string DbDateTime(string input)
{
return DateTime.ParseExact(input,
"MM/dd/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture).
ToString("yyyy-MM-dd h:mm:ss.fff");
}
From DateTime.ParseExact method
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.
M is for 1 to 12 but MM is for 01 to 12.
d is for 1 to 31 but dd is for 01 to 31.
Also be carefull about your hour forma. h is for 1 to 12, hh is for 01 to 12. If you want to use 24-hour format, you need to use H or HH formats.
For more informations, take a look;
Custom Date and Time Format Strings

date format issue, String was not recognized as a valid DateTime

I am getting dates from database and i want to convert the date in dd/MM/yyyy format,
I am trying this but it gives me error "String was not recognized as a valid DateTime."
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Please let me know how i can convert into dd/MM/yyyy format?
Thanks
try with MM/dd/yyyy hh:mm:ss tt instead of dd/MM/yyyy
if you use DateTime.ParseExact The format of the string representation must match a specified format exactly or an exception is thrown.
if you need only the date
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
var dateOnly= pDate.ToString("dd/MM/yyyy");
As per your comment, database contain data with Type 'Date', So you can read it directly as DateTime. You can convert to string by calling ToString with the expected Format.
2 steps:
DateTime pDate = DateTime.ParseExact("05/28/2013 12:00:00 AM", "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
return pDate.ToString("dd/MM/yyyy");
DateTime d = DateTime.Parse(
"15/12/2019",
new System.Globalization.CultureInfo("pt-BR"));
refer https://blog.submain.com/string-was-not-recognized-as-valid-datetime/

Categories

Resources