将日期时间12小时转换为日期时间24小时 [英] Convert datetime 12 hours to datetime 24 hours

查看:213
本文介绍了将日期时间12小时转换为日期时间24小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将12小时的日期时间转换为24小时的日期时间



如果我将datetime转换为字符串变量,我可以这样做(字符串strMaxFormat = modifiedDate.ToString( yyyy-MM-dd HH:mm:ss);)但是我想要日期时间变量





我的代码:

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:

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));





我尝试过:



string strMaxFormat = modifiedDate.ToString(yyyy-MM-dd HH:mm:ss);



What I have tried:

string strMaxFormat = modifiedDate.ToString("yyyy-MM-dd HH:mm:ss");

推荐答案

DateTime 对象不知道12或24小时格式。它只是表示特定时间的值。



我还建议您不要自己进行DST计算。使用提供的函数在本地时间和UTC之间进行转换(例如, DateTime.ToLocalTime()表示UTC时间值)。如果您需要其他时区的本地时间而不是Windows实际使用的时区,请使用 TimeZoneInfo.ConvertTime方法(DateTime,TimeZoneInfo,TimeZoneInfo)(系统) [ ^ ]。另请参见转换时区之间的时间 [ ^ ]。



存储时间和执行计算时,请始终使用UTC。只有在显示它们时才会根据需要转换为当地时间。如果不这样做,迟早会出现不一致。
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.


无需转换。你只需使用<$ href =https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110) ToString 方法.aspx>自定义日期和时间格式字符串 [ ^ ]。
There is no conversion necessary. you just use the ToString method with a Custom Date and Time Format Strings[^].


首先,您必须了解.Net中DateTime值的表示方式,读取这个 [ ^ ]:

First, you got to under how DateTime value is represented in .Net, read this[^]:
Quote:

DateTime值类型表示日期和时间,其值范围为午夜12:00,1月1日,000 Anno Domini或AD(也称为Common Era,或CE)到公元9999年12月31日晚上11:59:59 (CE)这些限制在MinValue和MaxValue字段中存储为只读值。

时间值以100纳秒为单位调用ed ticks,并且特定日期表示为公历中自公元0001年1月1日午夜12点(午夜)以来经过的刻度数。例如,刻度值31241376000000000L表示1月1日星期五,0100 12:00 00:00午夜。

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.



简单地说,数据时间值只是一个运行ticks [ ^ ]从1月1日的0滴答开始,000 $

运行此演示:


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);
	}
}

您得到:

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

回到你的问题,不,没有12小时或24小时的日期时间值,你在这个世界看到的许多日期时间格式是人造的,它们的存在是为了适应多元文化的需求。但是在.Net框架的情况下,它们的根值都在运行。

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.


这篇关于将日期时间12小时转换为日期时间24小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆