Click here to Skip to main content
15,904,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get a date in 'yyyy-mm-dd' format (2013-10-25) in a DateTime object using C#?


I am writing a API client Wrapper for C# where I have to implement an interface which accepts the date as an DateTime object in 'yyyy-mm-dd' format.

What I have tried:

I have looked for lot of solutions but couldn't find one. It can be achieved by converting the DateTime to string but I need it as DateTime object only.

With Parse and ParseExact it gives output which include time 00:00:00 also, but the requirement is only to ger 'yyyy-mm-dd' format as DateTime object. Can someone please help??

Thanks in Advance
Posted
Updated 26-Dec-18 20:11pm

1 solution

You can't: DateTime values don't have a format, they are stored as a number of ticks since a specific moment in time. They only acquire a format of any kind when they are converted to strings for presentation to the user in some way - and then the format would normally depend on the users own preferences, not on yours!

You can format a DateTime to year-month-day very simply when you need to show it, but you can't "associate" any format with a DateTime value.
C#
string formatted = myDateTime.ToString("yyyy-MM-dd");

Formatting a DateTime for display - format string description[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900