Datetime parse not working c# [duplicate] - c#

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 7 years ago.
I am trying to read date from excel using code
String ss = (String)w.Cells[2 + i, 4].Value2;
dRow[3] = DateTime.Parse(ss);
Code works when ss = "12/11/2015" but gives error
String was not recognized as a valid DateTime
when ss = "13/11/2015"
It gives error because month can not be 12 but it is taking date as month. This is what I think. Same code is working on other PC. Do I need to check my date time format or anything like date setting.

DateTime.Parse uses standard date and time format of your CurrentCulture settings by default.
Looks like your CurrentCulture has MM/dd/yyyy format as a short date format and since there is no month as 13 in Gregorian Calendar (which probably uses as a Calendar for your CurrentCulture), you get FormatExcetion.
You can use DateTime.ParseExact method to specify your format exactly like;
dRow[3] = DateTime.ParseExact("13/11/2015", "dd/MM/yyyy",
CultureInfo.InvariantCulture);
If you get this as an input and you want to parse it to DateTime, you have to know which format it has. Other than that, it can generate ambiguous scenarios.
For example; what 01/02/2015 should be parsed as? 1st February or 2nd January?
You shouldn't assume that every string you supplied perfectly parsed with DateTime.Parse method. It is not that smart.

Related

How to remove zeros from datetime string C# [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I'm getting from API DateTime.Date, that means Date without time, and sometimes I'm getting DateTime with full valid date, but in case when I'm getting value without Time and since property is type of DateTime it will include also time but with value of zeros.
So my string looks like this:
29/08/2019 00:00:00
How could I recognize when it's without valid Time and remove this 00:00:00 from my string?
Thanks guys
Cheers
Parse it an then use ToShortDateString or ToString("d"):
string result = DateTime.Parse("29/08/2019 00:00:00").ToShortDateString();
Sometimes I'm getting full DateTime value (with valid Time part), so I
need to recognize when it's with zeros and format it.. to short date
string, I can not use it in every case
Well, then compare the DateTime with it's own Date property:
string dateString = "29/08/2019 01:00:00"; // with this sample the 'if' will not be entered because there is a time portion
DateTime dt = DateTime.Parse(dateString);
if(dt.Date == dt)
{
// there is no time portion in this DateTime
dateString = dt.ToShortDateString();
}

C# How to convert date 25APR18 to 2018-04-26? [duplicate]

This question already has answers here:
.net: How to parse a date with this format: 08 Feb 2011 06:46
(3 answers)
Closed 4 years ago.
That question may be already been answered here, but I could not find any answers.
I'm trying to to convert the string 25APR18 to 2018-04-25.
I tried to do the following:
DateTime.ParseExact("25APR18", "yyyy-mm-dd", CultureInfo.InvariantCulture);
But it does not convert and gives me the error saying "String was not recognized as a valid DateTime."
I was not able to find the right solution.
Any suggestions?
You can use:
DateTime date = DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);
The string format is case sensitive representing the input format you want to match. Lower case mm represents minutes but you need month abbreviated name MMM and that's one issue with the code you posted. Full list of format specifiers can be found here.
Once you have a DateTime object you can retrieve the date as string with new format:
string formattedDate = date.ToString("yyyy-MM-dd");
Which produces:
2018-04-25
Your date parse code should be
DateTime.ParseExact("25APR18", "ddMMMyy", CultureInfo.InvariantCulture);

how to remove Time from MVC5 DateTime [duplicate]

This question already has answers here:
Display only date and no time
(11 answers)
Closed 8 years ago.
I`m new to asp.Net mvc5. I have a problem with DateTime formatting. I need to remove time from Date.
20/01/2015 12:00:00
Needs to be like this :
20/01/2015
I Tried this :
var dateOnly = date1.Date;
But it still get the time with date.
You'll always have some time in a DateTimeobject. But you can format the string output to have what you need. There is a difference between the inner data hold be the C# object (ie with time, even if 00:00) and the way you print it on screen.
Try:
var dateAsString = date1.ToString("dd/MM/yyyy")
Everything is in the MSDN Documentation.
If you want to format the date in a string, you can specify a custom format which excludes the time:
DateTime date = DateTime.Now;
string dateString = date.ToString("dd/MM/yyyy");
By default, .NET will include the time too when converting a DateTime object to a string.

Convert mm/dd/yyyy to JsonDate format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Converting .NET DateTime to JSON
How can I convert a date value in "mm/dd/yyyy hh:mm:ss" format to "/Date(1324414956395)/" format (Json Date).
I am passing the date format "mm/dd/yyyy hh:mm:ss" into a MVC controller action method and I need to compare that to another date in JsonDate format in the code.
Thanks for help.
dt.ToUniversalTime() won't be recognised when using DateTime?. DateTime? is essentially Nullable<DateTime>. What you need to do is use the Value property to retrieve the DateTime object
dt.Value.ToUniversalTime();
You can then use the code from this post (Jeff Meatball Yang's answer, not the accepted answer) with your nullable DateTime.
(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
Or
This superior solution from this link: Converting .NET DateTime to JSON
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
It seems that this is a bad way to compare dates, however. This is basically counting the ticks since 1970 (or something) and is going to be a pretty long and accurate number. Even if you needed to make certain that the dates matched down to a second it seems like it would be easier to convert all the universal time to mm/dd/yyyy format and then compare at that point.

How to get DateTime.Now() in YYYY-MM-DDThh:mm:ssTZD format using C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
datetime to string with time zone
This is one of the W3C standard date time format that I want to use in sitemap. This DateTime standard is:
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD where TZD = time zone designator (Z or +hh:mm or -hh:mm) (eg 1997-07-16T19:20:30+01:00)
I am using the following code to get the current DateTime in that format:
DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssTZD");
But this gives: 2011-08-10T02:27:20TZD
Apparently the DateTime.Now doesn't recognize "TZD" in the parameter. Please help. How can I get the current DateTime in this format?
Use the zzz format specifier to get the timezone offset as hours and minutes. You also want to use the HH format specifier to get the hours in 24 hour format.
DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz")
Result:
2011-08-09T23:49:58+02:00
Some culture settings uses periods instead of colons for time, so you might want to use literal colons instead of time separators:
DateTime.Now.ToString("yyyy-MM-ddTHH':'mm':'sszzz")
Custom Date and Time Format Strings
use zzz instead of TZD
Example:
DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz");
Response:
2011-08-09T11:50:00:02+02:00
Try this:
DateTime.Now.ToString("yyyy-MM-ddThh:mm:sszzz");
zzz is the timezone offset.

Categories

Resources