Click here to Skip to main content
16,005,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write this code "Obj.TransDate = DateTime.Today;"
its working but its pass date and time both but i want only DATE in specified format.
Posted
Updated 25-Jul-13 2:45am
v3

You can use the following code:

<per>string date = DateTime.Today.ToShortDateString();
 
Share this answer
 
Hello,
If you are using c#, then following code will solve your problem for sure::
VB
string date = string.Empty;
date = DateTime.Now.ToString();
date = date.Split(' ')[0];
MessageBox.Show(date);


-Abhishek Chopra
 
Share this answer
 
You can use following code to get the date and time separately.

DateTime now = DateTime.Now;
string date = now.GetDateTimeFormats('d')[0];
string time = now.GetDateTimeFormats('t')[0];
 
Share this answer
 
You need to convert it ...

C#
DateTime date1 = DateTime.Today;
DateTime dateOnly = date1.Date;
MessageBox.Show(dateOnly.ToString("d"));
 
Share this answer
 
C#
Obj.TransDate = DateTime.Today.Date;


See documentation at DateTime.Date Property[^]

Note: The .Date property returns the date with a time of 00:00:00. This is how dates without a time component are returned. Use the .Date property with a DateTime Data Type to access only the date part of the DateTime Data Type.
 
Share this answer
 
v4
Comments
Ashutosh_jha 25-Jul-13 8:58am    
i was use it but same its pass current date and 12:00:00 AM
Matt T Heffron 25-Jul-13 16:42pm    
As Mike said, that's the value of a DateTime .Date property.
If you're looking at the value in the debugger, don't worry about it. :)
If it is being displayed somewhere in your application, then you need to control the formatting to be the date only, as in Solution 2.
Sergey Alexandrovich Kryukov 25-Jul-13 12:56pm    
Exactly, a 5.
—SA

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