Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
[Absurdity]

Hello everyone,
i want to convert datetime.now to format yyyy/MM/dd but the converted format should also be in datetime,not in string.that is i dont want string.format method as it will return datetime as string.i want as datetime.Any help?
Posted
Updated 4-Oct-13 10:47am
v2
Comments
Harikamalina 5-Mar-13 1:32am    
take the string which show the date as string... convert it into date time using
Convert.ToDateTime(string Value);
Thanks7872 5-Mar-13 1:35am    
DateTime daten = Convert.ToDateTime(DateTime.ParseExact(DateTime.Now.ToString(), "G",
CultureInfo.GetCultureInfo("en-US")).ToString("yyyy/MM/dd"));
i have tried above code,but it is not giving me the desired output,like for today,the result should be 2013/05/03,but m not getting that....any suggessions?
Karthik Harve 5-Mar-13 1:37am    
Why you want to cast datetime object to string and again back to datetime ? why cant you use same datetime object ?
Thanks7872 5-Mar-13 2:21am    
because if i remove tostring("yyyy/MM/dd") than it is giving me the same string which i dont want....can you provide me the suggession to display datime.now in format e.g. 2003/03/23(that is 23rd march 2003)?
Richard MacCutchan 5-Mar-13 5:59am    
DateTime objects are values, they do not have any format. The formatting is only used when you convert them to string types. You can add formatting details in the ToString() method; look at the documentation for full details.

Hi,

Write like this

C#
string dtime = DateTime.Now.ToString("yyyy/MM/dd");

you'll sure get your desire date format.
 
Share this answer
 
Comments
Thanks7872 5-Mar-13 7:01am    
thanks dear for your response.but this will give me the date in STRING not in DATETIME,i dont want that...well,after short reseach,i came to know that its not possible with DATETIME,because format corresponds to strings only,so i have changed my issue accordingly....thanks again...
What I always wonder about such questions is:

If someone learns C# how can he/she miss the MSDN site? The .NET docu can be reached by just pressing F1 in VisualStudio on any framework element - and it brings you (context-sensitive) to the documentation:

So have a look at http://msdn.microsoft.com/en-us/library/system.string.format(v=vs.80).aspx[^] for string formatting.

before asking further questions of this kind - not that you can't get the answer here (as you see) - I think you will get your answer faster and learn more by reading the documentation. If something there is unclear - that would make a good question...
 
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