how to remove Time from MVC5 DateTime [duplicate] - c#

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.

Related

How to format string "1441/10/15" exactly same to DateTime C#? [duplicate]

This question already has answers here:
Format date in C#
(7 answers)
Closed 2 years ago.
I have string date in this format "1441/10/15" i want to change it to exact same format "1441/10/15" in datatime.
i tried to change it like this,
var yesy = Convert.ToDateTime(emp.datepicker);
but it is changing it but format is different that is "{15/10/41 12:00:00 ص}"
I need exact same format that is "1441/10/15"
thanks for your suggestion
EDITED:
I have string date "1441/10/15" how can i save it as it is in sql data base as datetime.
DateTime dt = DateTime.Parse(emp.datepicker);
string yesy = dt.ToString("yyyy/MM/dd");
Console.WriteLine(yesy);
Output = "1441/10/15"
I presume that "{15/10/41 12:00:00 ص}" is the output of yesy.ToString() in the code you didn't show to us.
Try yesy.ToString("yyyy'/'MM'/'dd").
For more information about possible output format, the doc is here as Bagus Tesa commented.

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

Convert string in to DateTime [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)

C# Formatting a user input to date only [duplicate]

This question already has answers here:
Date vs DateTime
(14 answers)
Closed 6 years ago.
In my simple program, I need the user to input a date of format dd-mm-yyyy. Then I need to store that date in a text file.
To get the date from the user I use
DateTime l_Date = DateTime.ParseExact(Console.ReadLine(), "d-M-yyyy", new CultureInfo("en-CA"));
However, it always gets formatted as 2017-07-08 12:00:00 AMin my text file and I have also tried it in debug and the above line of code does return the date + time. I want only the date.
Thanks in advance for any help!
Use the ToShortDateString method:
https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring(v=vs.110).aspx
If you wanna 'Date' part only. You can use ToShortDateString as #wablab said.
Also you can use ToLongDataString method.
Or use ToString with your format, for example: ToString("dd-MM-yyyy").
For more information, follow this link: MSDN Standard Date and Time Format Strings

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.

Categories

Resources