Click here to Skip to main content
15,891,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DateTime fromDate = DateTime.Parse(HttpContext.Current.Session["a"].ToString());

DateTime toDate = DateTime.Parse(HttpContext.Current.Session["b"].ToString());


I only want date format in both the variables, not time.

in my session variable there is value format like 1/2/2012 and 5/2/2012 respectively

if i do as,

fromDate = fromDate.Date;

it still gives both date and time format.
Posted
Updated 25-Oct-17 23:50pm

Look at the DateTime.Date[^] property:

C#
DateTime dt = DateTime.Now;
DateTime dateOnly = dt.Date;

Alternatively, you can format a DateTime in many, many ways for display: Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
Espen Harlinn 4-Sep-12 8:33am    
5'ed!
Pratik Bhuva 23-Sep-14 8:53am    
5ed:)
DateTime fromDate = DateTime.Parse(HttpContext.Current.Session["a"].ToString());

DateTime toDate = DateTime.Parse(HttpContext.Current.Session["b"].ToString());

fromDate=Convert.ToDateTime(fromDate.ToShortDateString());
same for toDate
 
Share this answer
 
fromDate = fromDate.Date;

This code is correct, since the DateTime class always has a time with it. If you use the .Date function it returns only the date by setting the time to 00:00:00. This is enough. For outputting only the date you may want to use the different patterns for the .ToString method:
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm[^]
 
Share this answer
 
Comments
Espen Harlinn 4-Sep-12 8:32am    
5'ed!
C#
DateTime fromDate = 
DateTime.Parse(HttpContext.Current.Session["a"]).ToString("dd/MM/yyyy");


this should help you
 
Share this answer
 
v3
.NET is not making difference between Date, Time and Datetime. It is a matter of usage.
C#
String.Format("{0:d/M/yyyy}", fromDate);

or
C#
fromDate.toString("d/M/yyyy");
 
Share this answer
 
v2
Comments
D-Kishore 4-Sep-12 3:07am    
fine mine 5 :)
RAJKIRAN SWAIN 9-Jan-14 7:23am    
54545
AmitGajjar 4-Sep-12 3:08am    
Five from mine. :)
RAJKIRAN SWAIN 9-Jan-14 7:23am    
545
Espen Harlinn 4-Sep-12 8:32am    
5'ed! :-D

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