This question already has answers here:
how to give format DateTime.Date?
(7 answers)
Closed 7 years ago.
I need only the date from a DateTime class so I use DateTime.Date,
but it gives me a Full DateTime format, like this:
DateTime date = new DateTime();
date.Date
give me:
+ date {12/01/2016 00:00:00} System.DateTime
I only need the:
date {12/01/2016}
12/01/2016 00:00:00 is the same as 12/01/2016 as a value.
If you wanna get 12/01/2016 as a string representation, you can use .ToString method like;
var str = date.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you wanna get this 12/01/2016 as a DateTime, simply you can't. A DateTime instance always has date and time part. That's why you see it as 12/01/2016 00:00:00 on debugger even if you set it's time part to midnight.
Related
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 1 year ago.
I am trying to get this date 4th April 2021 12:00 which is todays date into a DateTime object.
I am in the UK and the emulator's locale is set to GB summer time and the format looks correct in it's setting page.
I have a date as a string:
string dateTimeStr = "08-04-2021 12:00";
I have tried this:
t = DateTime.Parse(dateTimeStr);
I get a datetime object of
4th Aug 2021 12:00
How can I parse a date as a string to DateTime and be sure that the month and the day will not be swapped?
DateTime.ParseExact(dateTimeStr, "dd/MMM/yyyy hh:mm", CultureInfo.InvariantCulture);
you can use ParseExact and mention the date format as per your requirement.
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();
}
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.
This question already has answers here:
format date in c#
(7 answers)
Closed 8 years ago.
i have this string "1/20/2015 12:00:00 AM" and i need to convert in something like this "20/1/2015" without time and days first.
DateTime mydate = Convert.ToDateTime(myDateFromDB);
To convert date string of format "dd/MM/yyyy" to DateTime, should used DateTime.TryParseExact where you can specify the date format string to let the converter know about the Culture.
To get the Date alone, use myDateTimeObj.Date.
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.