民國年

  • 247
  • 0
  • 2023-05-24

民國年算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class TnYr
{
   public TnYr()
{
 //
 // TODO: Add constructor code here
 //
}
   public static DateTime DT(string YrDate)
   {
       DateTime RVal = new DateTime();
       if (YrDate.Length == 6) YrDate = "0" + YrDate;
       if (YrDate.Length == 7)
       {
           try
           {
               DateTime.TryParse((Convert.ToInt32(YrDate.Substring(0, 3)) + 1911).ToString() + "/" + YrDate.Substring(3, 2) + "/" + YrDate.Substring(5, 2), out RVal);
           }
           catch
           {
           }
       }
       return RVal;
   }
   
   public static string DateStr(string YrDate)
   {
       string RVal = "";
       if (YrDate.Length == 6) YrDate = "0" + YrDate;
       if (YrDate.Length == 7)
       {
           try
           {
               RVal = Convert.ToDateTime((Convert.ToInt32(YrDate.Substring(0, 3)) + 1911).ToString() + "/" + YrDate.Substring(3, 2) + "/" + YrDate.Substring(5, 2)).ToString("yyyy/MM/dd");
           }
           catch
           {
           }
       }
       return RVal;
   }
   public static string CV(string ADate)
   {
     string RVal = "";
       
           try
           {
               DateTime D = Convert.ToDateTime(ADate);
               string SD = D.ToString("yyyy/MM/dd"); 
               string Y = "0" + (D.Year -1911).ToString ();
               string M = SD.Split ('/')[1];
               string DD = SD.Split ('/')[2]; 
               RVal = Y.Substring (5-Y.Length,3) + M + DD;
           }
           catch
           {
           }
       
       return RVal;
   }
}