日期时间转换 [英] Date Time Conversions

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

问题描述

嗨 我有一个获取希伯来数字日期并找到相等的公历日期的函数,例如:
Hebrew: [3,1,5772] => 02.10.2011
我使用:
DateTime DateInGreg = new DateTime(5772, 1, 3, new System.Globalization.HebrewCalendar());

我的问题是希伯来年是a年,因此月份范围在1-13之间,但是实现了DateTime类型以仅处理1-12之间的月份!
尝试切换今天的希伯来语日期:
[这是a年.]
DateTime DateInGreg = new DateTime(5771, 13, 26, new System.Globalization.HebrewCalendar());
您将收到ArgumentOutOfRangeException.

如何解决此问题,以便我的职能部门也能够处理希伯来文leap年?
谢谢.

Hi I have a function which get hebrew numeric date and find the equal gregorian date, for example:
Hebrew: [3,1,5772] => 02.10.2011
I use:
DateTime DateInGreg = new DateTime(5772, 1, 3, new System.Globalization.HebrewCalendar());

My problem is when the hebrew year is a leap year, so that the month range goes between 1-13, but DateTime type is implemented to handle only monthes between 1-12 !
Try to switch today''s hebrew date:
[It''s a leap year..]
DateTime DateInGreg = new DateTime(5771, 13, 26, new System.Globalization.HebrewCalendar());
You''ll get an ArgumentOutOfRangeException.

How can I solve this problem so that my function will be able to handle hebrew leap years also?
Thanks.

推荐答案

此问题类似于埃塞俄比亚的压延机.在埃塞俄比亚压光机 [
This problem is similar to Ethiopian calender. In Ethiopian calender[^], a year contains 13 months, the first 12 months have 30 days,the last month has 5 or 6 days depends on the leap year. New year starts September 11 or 12 based on the leap year so and so forth. So the only solution to work with conversion of datetime from Gregorian to Ethiopian is to implement the System.Globalization.Calender abstract class. That is
public sealed class EthiopianCalender : Calendar
    {
        public override DateTime AddMonths(DateTime time, int months)
        {
            throw new NotImplementedException();
        }

        public override int GetHour(DateTime time)
        {
            return base.GetHour(time);
        }

        public override int GetSecond(DateTime time)
        {
            return base.GetSecond(time);
        }

        public override int GetMinute(DateTime time)
        {
            return base.GetMinute(time);
        }

        public override int GetDaysInYear(int year)
        {
            return base.GetDaysInYear(year);
        }

        public override DateTime AddDays(DateTime time, int days)
        {
            return base.AddDays(time, days);
        }

        public override DateTime AddHours(DateTime time, int hours)
        {
            return base.AddHours(time, hours);
        }

        public override DateTime AddMilliseconds(DateTime time, double milliseconds)
        {
            return base.AddMilliseconds(time, milliseconds);
        }
        public override DateTime AddMinutes(DateTime time, int minutes)
        {
            return base.AddMinutes(time, minutes);
        }

        public override DateTime AddSeconds(DateTime time, int seconds)
        {
            return base.AddSeconds(time, seconds);
        }

        public override DateTime AddWeeks(DateTime time, int weeks)
        {
            return base.AddWeeks(time, weeks);
        }

        public override int GetLeapMonth(int year)
        {
            return base.GetLeapMonth(year);
        }

        public override bool IsLeapDay(int year, int month, int day)
        {
            return base.IsLeapDay(year, month, day);
        }

        public override int GetDaysInMonth(int year, int month)
        {
            return base.GetDaysInMonth(year, month);
        }
        public override bool IsLeapMonth(int year, int month)
        {
            return base.IsLeapMonth(year, month);
        }

        public override bool IsLeapYear(int year)
        {
            return base.IsLeapYear(year);
        }

        public override int GetLeapMonth(int year, int era)
        {
            return base.GetLeapMonth(year, era);
        }
        public override DateTime AddYears(DateTime time, int years)
        {
            throw new NotImplementedException();
        }
        public override double GetMilliseconds(DateTime time)
        {
            return base.GetMilliseconds(time);
        }

        public override int GetMonthsInYear(int year)
        {
            return base.GetMonthsInYear(year);
        }

        public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
        {
            return base.GetWeekOfYear(time, rule, firstDayOfWeek);
        }
        public override int[] Eras
        {
            get { throw new NotImplementedException(); }
        }

        public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
        {
            return base.ToDateTime(year, month, day, hour, minute, second, millisecond);
        }

        public override int ToFourDigitYear(int year)
        {
            return base.ToFourDigitYear(year);
        }
        public override int GetDayOfMonth(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override DayOfWeek GetDayOfWeek(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetDayOfYear(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetDaysInMonth(int year, int month, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetDaysInYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetEra(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetMonth(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetMonthsInYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetYear(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapDay(int year, int month, int day, int era)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapMonth(int year, int month, int era)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
        {
            throw new NotImplementedException();
        }
    }


例如,a年的计算将为


For example a leap year calculation will be

public override bool IsLeapYear(int year, int era)
   {
       try
       {
           if (year < 1)
           {
               throw new ArgumentOutOfRangeException("year", year, "Year is outside bounds");
           }
       }
       catch (Exception exception)
       {
           throw exception;        
       }
       return ((year % 4) == 3);
   }


一旦完成了基于Hebrew压光机的压光机,就可以像使用Gregorian 压光机一样使用.即


Once you finished implement the calender based on Hebrew calender, you can use as the same way you use Gregorian calender. i.e

EthiopianCalender calender = new EthiopianCalender();
DataTime dateTime = new DateTime(5771, 13, 26, calender.GetHour(Now), calender .GetMinute(Now), calender.GetSecond(Now), new EthiopianCalender())



希望这对您有所帮助.



I hope this will help you as a starting point.


尝试使用日历本身进行转换:
Try using the calendar itself to do the conversion:
System.Globalization.HebrewCalendar hc = new System.Globalization.HebrewCalendar();
DateTime gd = hc.ToDateTime(5771,13,26,0,0,0,0,System.Globalization.HebrewCalendar.CurrentEra);





gd.ToString() == "9/25/2011 12:00:00 AM"


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

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