Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to Convert datetime 12 hours to datetime 24 hours

I can do that if i convert datetime to string variable( string strMaxFormat = modifiedDate.ToString("yyyy-MM-dd HH:mm:ss");) but i wanted datetime variable


My code:
C#
DateTime modifiedDate = DateTime.Now;
            bool isDayLightSaving = false;

DateTime unixGMTDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                    modifiedDate=unixGMTDateTime.AddMilliseconds(Convert.ToDouble(lastModifiedDate));
                    isDayLightSaving = objUTI.checkisDaylightSaving();
 //subtract 330 or 270 minutes based on daylight saving.. Subtraction is done to change time from UTC to local
int systemDTOffset = DateTimeOffset.Now.Minute;
if (isDayLightSaving)
modifiedDate = modifiedDate.AddMinutes(-(systemDTOffset));
else
  modifiedDate = modifiedDate.AddMinutes(-(systemDTOffset - 60));


What I have tried:

string strMaxFormat = modifiedDate.ToString("yyyy-MM-dd HH:mm:ss");
Posted
Updated 4-Jan-17 2:20am
v2
Comments
F-ES Sitecore 4-Jan-17 8:18am    
Dates only have a format when you convert them to a string, so just leave the DateTime as it is, the date and time you see when you hover over the variable in VS or write it out is simply the ToString value and the format of that is dictated by your OS settings. If you want the time in 24 hour format then specify that when you come to show it via ToString(format).

The DateTime object does not know about a 12 or 24 hour format. It is just a value representing a specific time.

I also suggest to not do DST calculations yourself. Use the provided functions to convert between local time and UTC (e.g. DateTime.ToLocalTime() for an UTC time value). If you need local times for other timezones than the one used actually by Windows use the TimeZoneInfo.ConvertTime Method (DateTime, TimeZoneInfo, TimeZoneInfo) (System)[^]. See also Converting Times Between Time Zones[^].

When storing times and performing calculations, do it always in UTC. Only when displaying them convert to local time if required. If not doing so you will get inconsistencies sooner or later.
 
Share this answer
 
There is no conversion necessary. you just use the ToString method with a Custom Date and Time Format Strings[^].
 
Share this answer
 
First, you got to under how DateTime value is represented in .Net, read this[^]:
Quote:
The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini, or A.D. (also known as Common Era, or C.E.) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) These limits are stored as read-only values in the MinValue and MaxValue fields.
Time values are measured in 100-nanosecond units called ticks, and a particular date is expressed as the number of ticks that have elapsed since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the Gregorian calendar. For example, a tick value of 31241376000000000L represents Friday, January 01, 0100 12:00:00 midnight.

To put it simply, the datatime value is just a running ticks[^] starting from 0 ticks on January 1, 0001.
Run this demo:
using System;
					
public class Program
{
	public static void Main()
	{
		DateTime dateTimeMin = DateTime.MinValue;
		
		DateTime dateTimeMax = DateTime.MaxValue;
		
		long minTicks = dateTimeMin.Ticks;
		
		long maxTicks = dateTimeMax.Ticks;
		
		Console.WriteLine("Min DateTime {0} has ticks of {1}", dateTimeMin, minTicks);
		
		Console.WriteLine("Max DateTime {0} has ticks of {1}", dateTimeMax, maxTicks);
	}
}
You get:
Min DateTime 1/1/0001 12:00:00 AM has ticks of 0
Max DateTime 12/31/9999 11:59:59 PM has ticks of 3155378975999999999

To get the number of ticks elapsed at any instance, do this
DateTime.Now.Ticks
Coming back to your question, No, there is neither 12 hrs nor 24 hrs datetime value, the many date time formats that you see in this world are man-made, they exist to fit in the diverse culture needs. But the root values of them all are running ticks in case of .Net framework.
 
Share this answer
 
v4

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