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

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

Related

Why this double value cannot be converted [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 3 years ago.
In the above code, I am getting the date data from SpreadsheetDocument. The exampled date cannot be converted.
But other dates can be converted.
Why cant i convert this date and how can I convert it in different way?
DateTime.FromOADate(double.Parse("05.09.1977"));
I want to convert this string to DateTime with this.
"05.09.1977 is not a valid double. It looks like it's an actual date (either MONTH.DAY.YEAR or DAY.MONTH.YEAR).
To parse it to a DateTime use either:
DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture)
or
DateTime.ParseExact("05.09.1977", "MM.dd.yyyy", CultureInfo.InvariantCulture)
This way:
Console.WriteLine(DateTime.ParseExact("05.09.1977", "dd.MM.yyyy", CultureInfo.InvariantCulture));
Output:
05/09/1977 00:00:00
according to the
DateTime.FromOADate(Double) method doc
Returns a DateTime equivalent to the specified OLE Automation Date.
public static DateTime FromOADate (double d);
Parameters
d
Double
An OLE Automation Date value.
so, you have to pass a double as argument
but double.Parse("05.09.1977")???
how can that be a double??
that is the reason

DateTime.date give me a full Date why? [duplicate]

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.

Datetime parse not working c# [duplicate]

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.

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.

Categories

Resources