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

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

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.

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 datetime to date in c#.Net but not a string date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reading Datetime value From Excel sheet
I want to compare a date (e.g. 18/10/2012 00:00:00) with an Excel date. If I am using datetime.toshortdatestring() the value gets converted to string and comparing a string with an Excel date doesnot work. Can you please suggest me some other workaround to this problem.
Would the DateTime.Date property work for you?
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

Converting datetime to z format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In C#, given a DateTime object, how do I get a ISO 8601 date in string format?
I've a variable with type DateTime in my C# code.
How do I convert this to "Z" format?
Thanks.
Do you mean converting to a string time/date like 2009-06-15 20:45:30Z? If so then try:
mydate.ToUniversalTime().ToString("u");
See http://msdn.microsoft.com/en-us/library/az4se3k1.aspx for info on Standard Date and Time format strings. In particular read the entry on using the "Universal Sortable ("u") Format Specifier" whcih explains why I have the ToUniversalTime call in there.
If you mean the universal sortable date/time pattern:
string formatted = theDate.ToString("u");
Example result:
2009-06-15 20:45:30Z
Based on a quick search it seems to me that you simply mean UTC time, in which case you can use the ToUniversalTime method of DateTime, and when displaying as a string append "Z" to the end, or use the Universal Format Specifier when calling ToString, or you could construct it using a number of format specifiers in the correct order.
I guess your format is ISO 8601: DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
Maybe a duplicate of this topic: Given a DateTime object, how do I get an ISO 8601 date in string format?

Categories

Resources