Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello EveryOne,

I have a very silly question in my mind,

I have declared a DateTime Variable like as below

DateTime curDate=DateTime.Now;

so this returns the output like 21-07-2014 03:45:45

but i want to remove the timestamp of the above date and also want to store it in variable curDate which i mention above

for ex:-

DateTime CurDate=21-07-2014


Note:- I know the method of converting dateTime to Date and store in string but i want to store in Datetime variable

is this possible?

thanks in advance
Posted
Comments
Atyant Srivastava 21-Jul-14 7:18am    
you cant store in datetime as it will have default value.
Tilak Rathour 21-Jul-14 7:19am    
ok thanks atyant

Sounds like there are two things being mixed up here.

Firstly, to get today's date use DateTime.Today rather than DateTime.Now. Alternatively, you can use DateTime.Now.Date.

This is still a DateTime and hence has a time component, but it is always midnight.

If you want to display this without the time component, then specify that in your string formatting.
 
Share this answer
 
The "DateTime" object always has a "Time" component, if you don't want to display it you can just do:
curDate.Date
which returns only the date component.

I would always suggest saving with the time component, and then just displaying the parts you need when you want to show it on screen.
 
Share this answer
 
Hi

Use ToShortDateString() method for DateTime class.

DateTime.Now.ToShortDateString().ToString()
 
Share this answer
 
Comments
Rob Philpott 21-Jul-14 7:41am    
ToShortDateString().ToString(). Convert a string to a string - why on earth?
You can convert datetime to format
DateTime dt=Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
 
Share this answer
 
Comments
lukeer 21-Jul-14 8:56am    
You have a DateTime and suggest to format it to a string and parse that right away back to DateTime? Really?

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