예제 #1
0
        /// <summary>
        /// 西元時間轉台灣文字時間
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static string DateToTaiwanDate(this DateTime datetime, int length = 7, bool _bool = false)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            if (datetime.Year <= 1911)
            {
                return(string.Empty);
            }
            if (length == 9)
            {
                if (!_bool)
                {
                    return(string.Format("{0}/{1}/{2}", taiwanCalendar.GetYear(datetime), datetime.Month.ToString().PadLeft(2, '0'), datetime.Day.ToString().PadLeft(2, '0')));
                }
                //$@"{taiwanCalendar.GetYear(datetime)}/{datetime.Month.ToString().PadLeft(2, '0')}/{datetime.Day.ToString().PadLeft(2, '0')}";
                else
                {
                    return(string.Format("{0}-{1}-{2}", taiwanCalendar.GetYear(datetime), datetime.Month.ToString().PadLeft(2, '0'), datetime.Day.ToString().PadLeft(2, '0')));
                }
                //$@"{taiwanCalendar.GetYear(datetime)}/{datetime.Month.ToString().PadLeft(2, '0')}/{datetime.Day.ToString().PadLeft(2, '0')}";
            }
            else if (length == 17)
            {
                return(string.Format("{0}/{1}/{2} {3}", taiwanCalendar.GetYear(datetime), datetime.Month.ToString().PadLeft(2, '0'), datetime.Day.ToString().PadLeft(2, '0'), datetime.ToString("HH:mm:ss")));
            }
            //$@"{taiwanCalendar.GetYear(datetime)}/{datetime.Month.ToString().PadLeft(2, '0')}/{datetime.Day.ToString().PadLeft(2, '0')} {datetime.ToString("HH:mm:ss")}";
            else
            {
                return(string.Format("{0}{1}{2}", taiwanCalendar.GetYear(datetime), datetime.Month.ToString().PadLeft(2, '0'), datetime.Day.ToString().PadLeft(2, '0')));
            }
            //$@"{taiwanCalendar.GetYear(datetime)}{datetime.Month.ToString().PadLeft(2, '0')}{datetime.Day.ToString().PadLeft(2, '0')}";
        }
예제 #2
0
        /// <summary>
        /// To the simple taiwan date.
        /// </summary>
        /// <param name="dateTime">The date time.</param>
        /// <returns>System.String.</returns>
        public static string ToSimpleTaiwanDate(this DateTime dateTime)
        {
            var taiwanCalendar = new TaiwanCalendar();
            var result         = $"{taiwanCalendar.GetYear(dateTime)}/{dateTime.Month}/{dateTime.Day}";

            return(result);
        }
예제 #3
0
        /// <summary>
        /// To the full taiwan date.
        /// </summary>
        /// <param name="dateTime">The datetime.</param>
        /// <returns>System.String.</returns>
        public static string ToFullTaiwanDate(this DateTime dateTime)
        {
            var taiwanCalendar = new TaiwanCalendar();
            var result         = $"民國 {taiwanCalendar.GetYear(dateTime)} 年 {dateTime.Month} 月 {dateTime.Day} 日";

            return(result);
        }
예제 #4
0
        /// <summary>
        ///  將西元日期轉換成簡易民國日期,可指定輸出單純年、年月或年月日
        /// </summary>
        /// <param name="datetime">西元年</param>
        /// <param name="type">1=年,2=年月,3=年月日</param>
        /// <returns>簡易民國年,如 0640501,年數不滿3碼,會自動補零至3碼</returns>
        public static string ToSimpleTaiwanDate(this DateTime datetime, int type = 3)
        {
            if (datetime == null)
            {
                return("");
            }

            var datetimeParse = new DateTime();

            if (!DateTime.TryParse(datetime.ToShortDateString(), out datetimeParse))
            {
                return("");
            }

            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();
            var            year           = taiwanCalendar.GetYear(datetime).ToString().Length <= 2 ? "0" + taiwanCalendar.GetYear(datetime).ToString() : taiwanCalendar.GetYear(datetime).ToString();
            var            month          = datetime.Month.ToString().Length <= 1 ? "0" + datetime.Month.ToString() : datetime.Month.ToString();
            var            day            = datetime.Day.ToString().Length <= 1 ? "0" + datetime.Day.ToString() : datetime.Day.ToString();

            switch (type)
            {
            case 1:
                return(string.Format("{0}", year));

            case 2:
                return(string.Format("{0}{1}", year, month));

            case 3:
                return(string.Format("{0}{1}{2}", year, month, day));
            }

            return(string.Format("{0}{1}{2}", year, month, day));
        }
        public void PosTest3()
        {
            System.Globalization.Calendar tc = new TaiwanCalendar();
            DateTime dt = tc.MinSupportedDateTime;

            Assert.Equal(tc.GetYear(dt), 1);
        }
예제 #6
0
        public String GetBirthDate(DateType type, DateFormat format)
        {
            string result = "";

            switch (type)
            {
            case DateType.Calendar:
                result = (format == DateFormat.ShortDate) ? this.birthDate.ToString("d") : this.birthDate.ToString("D");
                break;

            case DateType.ChineseCalendar:
                //System.Globalization.TaiwanCalendar
                TaiwanCalendar c1 = new TaiwanCalendar();
                int            y  = c1.GetYear(this.birthDate);
                int            m  = c1.GetMonth(this.birthDate);
                int            d  = c1.GetDayOfMonth(this.birthDate);
                result = (format == DateFormat.ShortDate) ? String.Format("{0:000}/{1:00}/{2:00}", y, m, d) : String.Format("民國{0}年{1}月{2}日", y, m, d);
                break;

            case DateType.ChineseLunisolar:
                //System.Globalization.TaiwanLunisolarCalendar
                TaiwanLunisolarCalendar c2 = new TaiwanLunisolarCalendar();
                int y2 = c2.GetYear(this.birthDate);
                int m2 = c2.GetMonth(this.birthDate);
                int d2 = c2.GetDayOfMonth(this.birthDate);
                result = (format == DateFormat.ShortDate) ? String.Format("{0:000}/{1:00}/{2:00}", y2, m2, d2) : String.Format("農曆{0}年{1}月{2}日", y2, m2, d2);
                break;

            default:
                break;
            }
            return(result);
        }
예제 #7
0
        public static string timeHelper(DateTime tDate)
        {
            TaiwanCalendar twC = new TaiwanCalendar();

            return(twC.GetYear(tDate) +
                   "." + twC.GetMonth(tDate) + "." + twC.GetDayOfMonth(tDate));
        }
예제 #8
0
        static public string GetTaiwanToday()
        {
            TaiwanCalendar twC = new TaiwanCalendar();

            return(twC.GetYear(DateTime.Today) + "/" + twC.GetMonth(DateTime.Today) + "/" + twC.GetDayOfMonth(DateTime.Today));
            //twC = null;
        }
        /// <summary>
        /// 將DateTime轉換成民國年
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="Format"></param>
        /// <returns></returns>
        public static string ToTaiwanDate(this DateTime dateTime, string Format)
        {
            TaiwanCalendar tc    = new TaiwanCalendar();
            Regex          regex = new System.Text.RegularExpressions.Regex(@"[yY]+");

            Format = regex.Replace(Format, tc.GetYear(dateTime).ToString("000"));
            return(dateTime.ToString(Format));
        }
예제 #10
0
        /// <summary>
        /// Convert the DateTime object to TaiwanCalendar DateTime format.
        /// For example: 2008/1/1 will become 97/1/1
        /// </summary>
        public static DateTime ToTaiwanDateTime(this DateTime d)
        {
            TaiwanCalendar tc = new TaiwanCalendar();

            DateTime result = new DateTime(tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d), d.Hour, d.Minute, d.Second, d.Millisecond);

            return(result);
        }
예제 #11
0
        public static string ToFullTaiwanDate(this  DateTime datetime)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return(string.Format("{0}{1:00}",
                                 taiwanCalendar.GetYear(datetime),
                                 datetime.Month));
        }
예제 #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            TaiwanCalendar taiwanCalender     = new TaiwanCalendar();
            DateTime       datetime           = DateTime.Parse(dateTimePicker1.Text);
            int            taiwanCalendarYear = taiwanCalender.GetYear(datetime);

            label3.Text = taiwanCalendarYear.ToString();
        }
예제 #13
0
        public String ToSimpleTaiwanDate(DateTime datetime) //轉民國
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return(string.Format("{0}/{1}/{2}",
                                 taiwanCalendar.GetYear(datetime),
                                 datetime.Month,
                                 datetime.Day));
        }
예제 #14
0
        /// <summary>
        /// To the simple taiwan date.
        /// </summary>
        /// <param name="datetime">The datetime.</param>
        /// <returns></returns>
        public static string ToSimpleTaiwanDate(this DateTime datetime)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return string.Format("{0}/{1:00}/{2:00}",
                taiwanCalendar.GetYear(datetime),
                datetime.Month,
                datetime.Day);
        }
예제 #15
0
        /// <summary>
        /// To the full taiwan date.
        /// </summary>
        /// <param name="datetime">The datetime.</param>
        /// <returns></returns>
        public static string ToFullTaiwanDate(this DateTime datetime)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return string.Format("民國 {0} 年 {1} 月 {2} 日",
                taiwanCalendar.GetYear(datetime),
                datetime.Month,
                datetime.Day);
        }
예제 #16
0
        /// <summary>
        /// To the simple taiwan date.
        /// </summary>
        /// <param name="datetime">The datetime.</param>
        /// <returns></returns>
        public static string ToSimpleTaiwanDate(this DateTime datetime)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return(string.Format("{0}/{1}/{2}",
                                 taiwanCalendar.GetYear(datetime),
                                 datetime.ToString("MM"),
                                 datetime.ToString("dd")));
        }
예제 #17
0
        public static string ToTaiwanDate(this DateTime datetime, bool ampm = false)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            Object[] year = new Object[] { taiwanCalendar.GetYear(datetime), datetime.ToString("MM"), datetime.Day, null, null, null };
            year[3] = (ampm ? String.Concat(datetime.ToString("tt", new CultureInfo("en-US")).ToUpper(), " ") : "");
            year[4] = (ampm ? datetime.ToString("hh") : datetime.ToString("HH"));
            year[5] = datetime.ToString("mm");
            return("{0}年{1}月{2}日 {3}{4}:{5}".ToFormat(year));
        }
예제 #18
0
파일: Util.cs 프로젝트: IreneGigabit/nOpt
    /// <summary>
    /// 轉成民國日期格式(yyy/mm/dd)
    /// </summary>
    public static string ToShortTwDate(this DateTime datetime, bool padZero)
    {
        TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

        if (padZero)
        {
            return(string.Format("{0}/{1}/{2}",
                                 taiwanCalendar.GetYear(datetime).ToString().PadLeft(3, '0'),
                                 datetime.Month.ToString().PadLeft(2, '0'),
                                 datetime.Day.ToString().PadLeft(2, '0')));
        }
        else
        {
            return(string.Format("{0}/{1}/{2}",
                                 taiwanCalendar.GetYear(datetime).ToString(),
                                 datetime.Month.ToString(),
                                 datetime.Day.ToString()));
        }
    }
예제 #19
0
        /// <summary>
        /// 取得民國年
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public string TaiwanDateYear(DateTime datetime)
        {
            if (datetime == DateTime.MinValue)
            {
                return(" ");
            }
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return(string.Format("{0:000}", taiwanCalendar.GetYear(datetime)));
        }
예제 #20
0
        private void rdoROC_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoROC.Checked == true)
            {
                txtDate.Text = "yyy年MM月dd日";

                TaiwanCalendar cal    = new TaiwanCalendar();
                int            twYear = cal.GetYear(DateTime.Now);
                txtDateShow.Text = String.Format("{0:D3}年{1:D2}月{2:D2}日", twYear, DateTime.Now.Month, DateTime.Now.Day);
            }
        }
예제 #21
0
        /// <summary>
        /// 取得民國年(數值樣式ex:1911→-01;1912→001;1913→002)
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToClaimTaiwanYearNumber(this DateTime dateTime)
        {
            var taiwanCalendar = new TaiwanCalendar();

            if (dateTime >= new DateTime(1912, 1, 1))
            {
                return(taiwanCalendar.GetYear(dateTime).ToString("D3"));
            }
            else
            {
                return("-" + Math.Abs(dateTime.Year - 1912).ToString(CultureInfo.CurrentCulture).PadLeft(2, '0'));
            }
        }
예제 #22
0
        /// <summary>
        /// 輸入西元年yyymmdd,輸出民國年yyymmdd
        /// </summary>
        /// <param name="dateStr">yyyymmdd</param>
        /// <returns>yyymmdd</returns>
        public static string JW_2TaiwanDateStr(this String dateStr)
        {
            int            yy             = Convert.ToInt16(dateStr.Substring(0, 4));
            int            mm             = Convert.ToInt16(dateStr.Substring(4, 2));
            int            dd             = Convert.ToInt16(dateStr.Substring(6, 2));
            DateTime       datetime       = new DateTime(yy, mm, dd);
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();

            return(string.Format("{0}{1:00}{2}",
                                 taiwanCalendar.GetYear(datetime),
                                 datetime.Month,
                                 datetime.Day));
        }
예제 #23
0
    public static string ToLongTaiwanDate(this DateTime dateTime, bool HasBlank = false)
    {
        if (DateTime.Compare(dateTime, new DateTime(1912, 1, 1)) < 0)
        {
            return("");
        }

        TaiwanCalendar TC = new TaiwanCalendar();

        if (HasBlank)
        {
            return(string.Format("民國 {0} 年 {1} 月 {2} 日"
                                 , TC.GetYear(dateTime)
                                 , dateTime.Month
                                 , dateTime.Day));
        }

        return(string.Format("民國{0}年{1}月{2}日"
                             , TC.GetYear(dateTime)
                             , dateTime.Month
                             , dateTime.Day));
    }
예제 #24
0
    public static string ToShortTaiwanDate(this DateTime dateTime, string Split = "")
    {
        if (DateTime.Compare(dateTime, new DateTime(1912, 1, 1)) < 0)
        {
            return("");
        }
        TaiwanCalendar TC = new TaiwanCalendar();

        return(string.Format("{0}" + Split + "{1}" + Split + "{2}"
                             , TC.GetYear(dateTime)
                             , dateTime.Month.ToString("00")
                             , dateTime.Day.ToString("00")));
    }
예제 #25
0
파일: Program.cs 프로젝트: baihu7851/c
 static string TaiwanTimeChange(string input)
 {
     try
     {
         DateTime       date           = Convert.ToDateTime(input);
         TaiwanCalendar taiwanCalendar = new TaiwanCalendar();
         return($"民國 {taiwanCalendar.GetYear(date)}年 {date.Month}月 {date.Day}日");
     }
     catch
     {
         return("-1");
     }
 }
예제 #26
0
    public static string ToShortTaiwanDateTime(this DateTime dateTime)
    {
        if (DateTime.Compare(dateTime, new DateTime(1912, 1, 1)) < 0)
        {
            return("");
        }
        TaiwanCalendar TC = new TaiwanCalendar();

        return(string.Format("{0}/{1}/{2} {3}"
                             , TC.GetYear(dateTime)
                             , dateTime.Month
                             , dateTime.Day
                             , dateTime.ToString("HH:mm:ss")));
    }
예제 #27
0
파일: DateUtil.cs 프로젝트: BiancoLee/ALPHA
        public static String getCurChtDateTime(int yearLen)
        {
            //String strFormat = "yyyyMMdd HHmmssff";
            //DateTime now = DateTime.Now;
            //String strDateTime = now.ToString(strFormat);
            //strDateTime = (now.Year - 1911).ToString().PadLeft(yearLen, '0') + strDateTime.Substring(4, strDateTime.Length - 4);
            //return strDateTime;

            TaiwanCalendar twCalender  = new TaiwanCalendar();
            DateTime       now         = DateTime.Now;
            string         strDateTime = $"{twCalender.GetYear(now).ToString().PadLeft(yearLen, '0')}{now.Month}{now.Day}";

            return(strDateTime);
        }
예제 #28
0
        public static string ToFullTaiwanDate(this HtmlHelper helper, DateTime datetime)
        {
            TaiwanCalendar taiwanCalendar = new TaiwanCalendar();
            var            EndDate        = datetime.ToShortDateString().ToString();
            string         useless        = "9999/9/9";

            if (EndDate.Equals(useless))
            {
                return(string.Format("  年  月  日"));
            }
            return(string.Format(" {0} 年 {1} 月 {2} 日",
                                 taiwanCalendar.GetYear(datetime),
                                 datetime.Month,
                                 datetime.Day));
        }
예제 #29
0
        /// <summary>
        /// change datetime to Taiwan Datetime
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="format">string format like "中華民國{0}年{1}月{2}日"</param>
        /// <param name="padLeftCount">年份要預留幾位數 像是 99年 或 099 年</param>
        /// <param name="padchar">補齊位數符號 default = 0</param>
        /// <param name="defaultDateTime"></param>
        /// <returns></returns>
        public static string AsTaiwanDateTime(this DateTime dateTime, string format, int padLeftCount, char padchar = '0',
                                              DateTime defaultDateTime = default(DateTime))
        {
            if (dateTime == null)
            {
                return(defaultDateTime.ToShortDateString());
            }

            DateTime       result = defaultDateTime;
            TaiwanCalendar tai    = new TaiwanCalendar();
            string         tmp    = string.Format(format,
                                                  tai.GetYear(dateTime).AsString().PadLeft(padLeftCount, padchar),
                                                  tai.GetMonth(dateTime),
                                                  dateTime.Day);

            return(tmp);
        }
예제 #30
0
        /// <summary>
        /// 取得民國年(國字樣式ex:1911→前1;1912→元;1913→2)
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToClaimTaiwanYear(this DateTime dateTime)
        {
            var taiwanCalendar = new TaiwanCalendar();

            if (dateTime >= new DateTime(1913, 1, 1))
            {
                return(taiwanCalendar.GetYear(dateTime).ToString());
            }
            if (dateTime >= new DateTime(1912, 1, 1))
            {
                return("元");
            }
            else
            {
                return("前" + Math.Abs(dateTime.Year - 1912).ToString(CultureInfo.CurrentCulture));
            }
        }