Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to get the date in mm/dd/yyyy with hrs:min:sec but the hrs mins sec should always be in this kind of formats 13:18:46 if afternoon,10:48:00 if morning.How can i do this
also i need to get todays date in the same format


01/29/2018 13:18:46
01/07/2017 10:48:00
06/07/2017 11:38:00


What I have tried:

i tried so many codes but they are not giving me the correct format that i need
Posted
Updated 29-Jan-18 3:30am
Comments
phil.o 29-Jan-18 8:30am    
I'm quite unsure about your question. Do you want to get a DateTime value from an input string (that is called a parsing operation), or do you want to get a specific string representation for a DateTime value you already have?
kav@94 29-Jan-18 8:58am    
when i choose date using datepicker i need to get the date into string variable in these formats
01/29/2018 13:18:46
01/07/2017 10:48:00
06/07/2017 11:38:00

and also todays date should be in the above formats
Richard MacCutchan 29-Jan-18 9:34am    
No you don't want it as strings. Please re-read my comments below, and use the proper methods for Date and Time handling.

 
Share this answer
 
v2
Comments
kav@94 29-Jan-18 8:50am    
but they are not working could you show with some example for today's date and the date entered in textbox it should be of the above mentioned formats
Don't use text boxes to get date input, use the proper calendar controls that are freely available.
 
Share this answer
 
Comments
kav@94 29-Jan-18 8:50am    
but they are not working could you show with some example for todays's date and the date entered in textbox it should be of the above mentioned formats
Richard MacCutchan 29-Jan-18 8:56am    
You need to deal with dates properly as DateTime class objects. Do not use text strings except when the values need to be displayed or printed. Go to DateTime Structure (System)[^] for date/times, and Calendar Class (System.Web.UI.WebControls)[^] to learn how to use this control.
kav@94 30-Jan-18 0:22am    
i am already using calander control only but i am getting the date only in mm/dd/yyyy format but i need to get in these formats 01/29/2018 13:18:46
01/07/2017 10:48:00,06/07/2017 11:38:00
Richard MacCutchan 30-Jan-18 3:58am    
How many times must I repeat this? Do not use strings to manipulate dates and times in your code. The only time you need to convert them to strings is when you want to display them to the user. If you are manipulating them as strings inside your code then you are doing it completely wrong, and will most likely have problems with your programs.
If you are trying it out with ASP.Net and C# then below solution can help you out to start.

[Asp.Net Calendar control with Time]

If you need a control in Win form and c# then below is an example you can look out.

C# DateTimePicker Control[^]

For the Win form example you need to add three drop downs as you can see it was done in Asp.net solution and proceed with your solution.

N.B: Please don't copy paste the samples provided .Go through the examples try to see how they are achieving their need and then you atry to implement yours with certain changes required.

Tha above solution provided are also looks good for me:)
 
Share this answer
 
Comments
kav@94 30-Jan-18 0:22am    
i am already using calander control only but i am getting the date only in mm/dd/yyyy format but i need to get in these formats 01/29/2018 13:18:46
01/07/2017 10:48:00,06/07/2017 11:38:00
[no name] 30-Jan-18 1:05am    
Then for that you should have a time control like dropdown lists for hours minutes and seconds.From calendar control you can't get time.Only date is possible.
Richard MacCutchan 30-Jan-18 3:59am    
Yes you can get time values: see the documentation.

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