Share via


Conversion of Datetime from 12 hours to 24 hours format

Question

Monday, December 26, 2011 6:54 AM

Hi,

double timestamp = 1113211532;

System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

 dateTime = dateTime.AddSeconds(timestamp);

 string printDate = dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString();

 MessageBox.Show(printDate);

Here the 4/11/2005 9.25 AM is the output which i am getting 

bur the time format i require is 24 hours time instead of 12 hours time.

please reply me with a solution

All replies (18)

Monday, December 26, 2011 10:20 AM âś…Answered

kiran ,

how you know a date field has 24 hour format or 12 hours?

it comes only when you display ,how you are displaying the date time,

i already mention that date time default is  24 hour.

 

By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 7:03 AM

Hiican you please use this one.

var sx = dateTime.ToString("dd/MM/yyyy HH:mm:ss")

By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 7:08 AM

Update your code like this,

double timestamp = 1113211532;
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp);
MessageBox.Show(dateTime.ToString("MM/dd/yyyy HH:mm"));

 

Please mark this post as answer if it solved your problem. Happy Programming!


Monday, December 26, 2011 7:13 AM

Hi adavesh it is wrong

it should be

MM/dd/yyyy HH:mm

By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 9:00 AM

But i want return value Datetime Datatype instead of string.


Monday, December 26, 2011 9:04 AM

Hi Kiran_msk

you may use either following format for 24 hours.

MM/dd/yyyy HH:MM:ss
dd/MM/yyyy HH:MM:ss


Monday, December 26, 2011 9:09 AM

what it means ?

datetime object defaultly in 24 hours format,

if you want to print or use any where you need format how you need.

can you elaborate exactly what you need?

By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 9:29 AM

Hi adavesh it is wrong

it should be

MM/dd/yyyy HH:mm

By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

Yeah.. a typo :) Thanks.Please mark this post as answer if it solved your problem. Happy Programming!


Monday, December 26, 2011 9:34 AM

you are welcome dude we confused sometimes.
By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 9:38 AM

But i want return value Datetime Datatype instead of string.

DateTime object will always be in the format set by current culture (in regional settings). You can't create seperate DateTime object for 12 hours and 24 hour formats. That will come into picture only when you want to display the time to user.

So, don't worry about creating 24Hour or 12Hour formatted datetime. Just format the the DateTime object to display in any format when you want to show it to user.

I hope this makes sense to you.

Please mark this post as answer if it solved your problem. Happy Programming!


Monday, December 26, 2011 9:54 AM

var sx = dateTime.ToString("dd/MM/yyyy HH:mm:ss")
 Here the return value is string datatype.
But I want this value as Datetime datatype.

Monday, December 26, 2011 10:05 AM

You didn't understand what I have written above. You already have the DateTime which is nothing but "dateTime". Anyways, let me re-write your original question,

double timestamp = 1113211532;
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp); //======================> Here is the DateTime object you need.

So, this is the DateTime object what you need. The stuff like .ToString("dd/MM/yyyy HH:mm:ss") has to be done only when you are showing the value of dateTime to the user. I hope this makes you understand.

Please mark this post as answer if it solved your problem. Happy Programming!


Monday, December 26, 2011 10:13 AM

Example:

The result is 8/23/2011 11:54:12 PM

But I need the result as 8/23/2011 23:54:12


Monday, December 26, 2011 10:16 AM

See. This is exactly what I am telling. You have DateTime object with value " 8/23/2011 11:54:12 PM". Now you want to display that value to user. So, just use ToString("dd/MM/yyyy HH:mm:ss") method on the DateTime object which returns you DateTime string in 24 Hour format. And then display that string to user.

Please mark this post as answer if it solved your problem. Happy Programming!


Monday, December 26, 2011 10:19 AM

I am not displaying to user.I need to save in database.In database the column is DateTime datatype.


Monday, December 26, 2011 10:39 AM

what is the problem if you pass the date into sql  with out 24 hour format?
By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Monday, December 26, 2011 11:06 AM

Hello,

What is the actual problem ? what are you trying to do ?

I'm guessing but you may want to use SqlDateTime and pass it a DateTime, alternatively you can use SqlParameter and set SqlDbType to DateTime.

Eyal (http://shilony.net), Regards.


Monday, December 26, 2011 11:17 AM

Thanks san