1

I have todayDate variable which data type is DateTime and string variable dateString. i convert dateString value into DateTime Data type without Time.

string stringDate = "2018-05-07"
DateTime todayDate = Convert.ToDateTime(stringDate);

when I convert stringData into DateTime, todayDate value is "5/7/2018 12:00:00 AM". I need todayDate value is "2018-05-07" format only.

Prashant Patel
  • 272
  • 2
  • 14
  • 2
    Use `DateTime.ToString` to get a format because a `DateTime` has no format. If you just want to show the date there is also the method `ToShortDateString` – Tim Schmelter May 07 '18 at 09:16
  • `DateTime` represents both Date and Time Value – user1672994 May 07 '18 at 09:18
  • 1
    Because `DateTime` stores a number, not a human-readable date string. Any human-readable representation is done by converting it to a string. By default, it uses the thread/machine culture's date/time format. If you want a specific string format, you have to use that format via `ToString(...)`. Read [my answer to another question](https://stackoverflow.com/a/50065742/3181933) for more info. – ProgrammingLlama May 07 '18 at 09:19
  • @Tim ToShortDateString is not working – Prashant Patel May 07 '18 at 09:25
  • @PrashantPatel: Why? `string stringTodayDate = DateTime.Today.ToShortDateString();` – Tim Schmelter May 07 '18 at 09:26
  • @Tim "2018-05-07" is date of example not today date get – Prashant Patel May 07 '18 at 09:27
  • @PrashantPatel: cmon, `Convert.ToDateTime("2018-05-07").ToShortDateString()` – Tim Schmelter May 07 '18 at 09:28
  • A `DateTime` value does not have **a** format, it's a value that you **can** format, but you have to ask for it. – Lasse V. Karlsen May 07 '18 at 09:30
  • @Tim First of all thanks for help me. i try DateTime todayDate = Convert.ToDateTime(stringDate).ToShortDateString(); but getting error. cannot convert string to datetime. i need only date in todayDate variable. is it possible? – Prashant Patel May 07 '18 at 09:31
  • @PrashantPatel: sure, because `ToShortDateString` returns a `string` and not a `DateTime` :) – Tim Schmelter May 07 '18 at 09:34
  • @PrashantPatel: _" i need only date in todayDate variable."_ No, that's impossible. Because a `DateTime` **always** contains a time even if it's `00:00:00`. You are asking for a string that represents a `DateTime`. Read what John, Lasse and others already told you. – Tim Schmelter May 07 '18 at 09:35
  • @Tim Thank you.. – Prashant Patel May 07 '18 at 09:39

1 Answers1

1

A DateTime object can be displayed with a format but a DateTime has no format. The format is specified when you try to convert a DateTime object to as String.

When you see your variable in Visual Studio, it is possible that your variable is displayed using american date format with a time part.

If you want to convert your date in yyyy-MM-dd format, you can use following code

string sFormattedDate = todayDate.ToString("yyyy-MM-dd");

The 12 hours notation in a little ambiguous for people that normally uses 24 hours notation because 12H AM are equal to 0H in 24 hours mode !

schlebe
  • 3,387
  • 5
  • 37
  • 50
  • 1
    or `todayDate.ToString("d")` or `todayDate.ToShortDateString()` – Tim Schmelter May 07 '18 at 09:38
  • @Tim It is still [culture-sensitive](https://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring(v=vs.110).aspx#Anchor_1), though. – ProgrammingLlama May 07 '18 at 09:54
  • 1
    @john: sure, but i haven't noticed that OP wants to prevent it. I'm pretty sure he just didn't understand that the debugger displays it always with time. If you look [here](https://en.wikipedia.org/wiki/Date_and_time_notation_in_India) in india there is already the desired format using `-` as date delimiter. – Tim Schmelter May 07 '18 at 09:55
  • @Tim Oh, I agree :) I just thought it best to point it out for anyone else reading the question in the future. – ProgrammingLlama May 07 '18 at 09:59