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


Here my problem is,
I gave take variable dt as datetime,
and when i insert only date then its display like 3/21/2012 12:00:00 AM format,
But i don't want time in this situation(i.e. when i don' insert time then it should be display only date).



Plz suggest me
Posted

Use the DateTime object's TimeOfDay method to get the number of ticks since midnight. If the return indicates that it is midnight, then you leave the time out when displaying the date (using ToString and passing the appropriate date format string).

Note: Updated based on additional information provided in comments.
 
Share this answer
 
v2
Comments
madhuri@mumbai 21-Mar-12 9:41am    
no,i cannot do this,
first i want to check that time ="12:00:00 AM"
then n then i want to convert into date format
Marc A. Brown 21-Mar-12 9:45am    
I'm not sure I understand what you're trying to do. If you're wanting to display date and time except when the time is midnight, then do just that -- check the time and if it's midnight, do what I said, otherwise display the whole thing.
If I'm still not understanding, please clarify.
madhuri@mumbai 21-Mar-12 9:49am    
yes,i don't want to disply midnight time when i will not insert time in datetime variable.and if i insert time it will display inserted time.
I want a method to retrievw time from datetime.
Marc A. Brown 21-Mar-12 9:51am    
Ok then. Use the TimeOfDay method. This returns the number of ticks since midnight. Does that help?
EDIT: TimeOfDay is a method of your DateTime object.
Sergey Alexandrovich Kryukov 21-Mar-12 16:28pm    
Sure, a 5.
By the way, it looks like this is the only correct answer at this moment of time.
These days, not only inquirers, but "experts" also spam... sad but true...
--SA
C#
string s = "Your date";


            DateTime.TryParse(s, out dt);

            string Time = dt.ToString("HH:mm:ss:tt");

          Or

            string t1 = dt.ToString("H:mm");


you can get date

string strdate= String.Format("{0:dd,MM,yyyy}", Your date goes here);
 
Share this answer
 
v2
Comments
[no name] 13-Jun-14 6:16am    
Do you think that it's possible that in the last 2 years, the OP has resolved whatever his issue actually was?
goathik 20-Jun-14 13:30pm    
did you just rate a solution with one star just because A MEMBER doesn't need it anymore? Do you really think none else would browse this question via google?

His answer is correct and I'll rate it 5 stars. If the question is still open, blame the admins.
HTML
DateTime dt = DateTime.Now; 
dt.ToString("dd-MMM-yyyy"); // This Store date in 12/Jun/2014 like format
 
Share this answer
 
Comments
Nirav Prabtani 13-Jun-14 6:23am    
Do you think this member need an answer after two years??
question posted date is 21-Mar-12 19:00pm
goathik 20-Jun-14 13:28pm    
did you just rate a solution with one star just because A MEMBER doesn't need it anymore? Do you really think none else would browse this question via google?

His answer is correct and I'll rate it 5 stars. If the question is still open, blame the admins.
Hi,

Use Convert Like,

DateTime dt = DateTime.Now; 
Convert.ToDateTime(dt).ToString("dd-MMM-yyyy");


Hope This May Help You...
 
Share this answer
 
 
Share this answer
 
I think you want to store only the date or time, right?
Anyway, to store the current time to a string variable, you can use this code:
C#
DateTime dt = DateTime.Now;
string time = dt.ToShortTimeString();

And you can simple print the output (If you're using console application)
C#
Console.WriteLine(time);

And you can store the date by using this:
C#
DateTime dt = DateTime.Now;
string date = dt.ToShortDateString();

And you can simple print it like the time variable.
What we have done is that we have stored in the string variable the time or date in the string format. Simple enough?
 
Share this answer
 
use this

C#
Console.WriteLine("The current time: {0: HH:mm:ss zzz}",
                   dt);



--RA
 
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