.Net C # conversion time timestamp

Much gossip that directly on the code

    ///  <Summary> 
    /// time expansion
     ///  </ Summary> 
    public  static  class DateTimeExtension 
    { 
        ///  <Summary> 
        /// Get timestamp
         ///  </ Summary> 
        ///  <param name = " dt "> time </ param> 
        ///  <param name =" millisecond "> whether millisecond, true millisecond (default) </ param> 
        ///  <Returns> </ Returns> 
        public  static  Long ToTimestamp ( the this dt the DateTime, BOOL millisecond = to true ) 
        { 
            / *
             * Ticks in units of 100 ns, 1 Tick = 100 ns, the divide by 10,000,000 (s), 10000 (ms) 
             * S (seconds), MS (millisecond), [mu] S (microseconds), NS (ns ), 1S = 1000ms, MS 1 = 1000 [mu], 1 [mu] = 1000ns 
             * / 
            int divisor = millisecond? 10000 : 10000000 ;
             Long dt_ticks = dt.ToUniversalTime () Ticks;.
             return (dt_ticks - Const.TiksUtc1970) / divisor; 
        } 

        // /  <Summary> 
        /// stamp converted into time, returns NULL then the conversion fails (e.g., the timestamp is invalid)
         ///  </ Summary> 
        ///  <param name = "timestamp"> timestamp </ param> 
        / //  <param name = "millisecond"> whether milliseconds,true millisecond (default)</ param> 
        ///  <param name = "localTime"> whether to output the local time, true local time (default value) </ param> 
        ///  <returns A> </ returns A> 
        public  static DateTime? ToDateTime ( the this  Long timestamp , BOOL millisecond = to true , BOOL localTime, = to true ) 
        { 
            the try 
            { 
                int MS = millisecond? 10000 : 10000000 ;
                 var dt = new new the DateTime (Const.TiksUtc1970 timestamp * + MS, DateTimeKind.Utc);
                 IF (localTime,)
                    dt.ToLocalTime (); 
                return dt; 
            } 
            the catch 
            { 
                return  null ; 
            } 
        } 

        ///  <Summary> 
        /// stamp is converted into time
         ///  </ Summary> 
        ///  <param name = "timestamp"> Time stamp </ param> 
        ///  <param name = "millisecond"> whether millisecond, true millisecond (default) </ param> 
        ///  <param name = "localTime,"> whether the output of the local time, true local time (default) </ param> 
        ///  <Returns> </ Returns>
        public static DateTime? ToDateTime(this string timestamp, bool millisecond = true, bool localTime = true)
        {
            if (long.TryParse(timestamp, out long ts))
            {
                return ts.ToDateTime(millisecond, localTime);
            }
            return null;
        }
    }
    ///  <Summary> 
    /// constant defines
     ///  </ Summary> 
    public  class Const 
    { 
        ///  <Summary> 
        /// date and time format
         ///  </ Summary> 
        public  const  String DateTimeFormatString = " YYYY- dd HH-the mM: mm: SS " ; 

        ///  <Summary> 
        /// date and time format
         ///  </ Summary> 
        public  const  String DateHmFormatString = " YYYY the mM-dd-HH: mm " ; 

        ///  < Summary> 
        /// date format
        /// </summary>
        public const string DateFormatString = "yyyy-MM-dd";

        /// <summary>
        /// utc 1601-1-1 到 utc 1970-1-1 的 Ticks
        /// </summary>
        public const long TiksUtc1970 = 621355968000000000;
    }

Guess you like

Origin www.cnblogs.com/bugzero/p/12077436.html