C # About time (gets the current timestamp in a specific format and in a variety of ways)

Source: Internet
Author: User

First, get the current time and switch to a specific format:

String tradetime = DateTime.Now.ToString ("Yyyymmddhhmmss", datetimeformatinfo.invariantinfo);

Set the time format you want:

String tradetime = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss", datetimeformatinfo.invariantinfo);

String tradetime = DateTime.Now.ToString ("yyyy mm month DD Day hh when mm minute ss seconds", Datetimeformatinfo.invariantinfo);

Second, get the current timestamp (the time to get the timestamp is 13 digits, only seconds to the time stamp is 10 bits)

The first method
///<summary>  
        ///Gets the current timestamp  
        ///</summary>  
        ///<param name= "Bflag" > Gets a 10-bit timestamp when true, and gets a 13-bit timestamp when false. bool Bflag = true</param>  
        ///<returns></returns> public  
        static String Gettimestamp (bool bflag)
        {
            TimeSpan ts = datetime.utcnow-new DateTime (1970, 1, 1, 0, 0, 0, 0);
            string ret = string. Empty;
            if (bflag)
                ret = Convert.toint64 (ts. totalseconds). ToString ();
            else
                ret = convert.toint64 (ts. TotalMilliseconds). ToString ();

            return ret;
        }

In general one project will be only one, or 10-bit or 13-bit, so you can also directly use the following code

public static long Gettimestamp ()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime ()-New DateTime (1970, 1, 1);//tou Niversaltime () to the time of the standard time zone, remove the words directly with the Beijing Time return
            (long) ts. TotalMilliseconds; Accurate to millisecond
            //return (long) ts. totalseconds;//get 10-bit
        }

The only difference in the following way is whether you need to get an exception.

Generate UNIX format time public
        static Long Getunix ()
        {
            try
            {
                TimeSpan TimeSpan = datetime.utcnow-new DateTime (1970, 1, 1);
                Return (long) TimeSpan. totalseconds;//gets the 10-bit return
                (long) TimeSpan. totalmilliseconds;
            }
            catch (Exception)
            {
                return-1
            }
        }

The first four complete methods and the results of operation

public static long Gettimestamp () {TimeSpan ts = DateTime.Now.ToUniversalTime ()-New DateTime (1970, 1, 1),//touniversaltime () to the time of the standard time zone, remove the words directly with the Beijing Time return (long) TS. TotalMilliseconds; Accurate to millisecond//return (long) ts.
            totalseconds;//get 10-bit}//generate UNIX format time public static long Getunix () {try
                {TimeSpan TimeSpan = datetime.utcnow-new DateTime (1970, 1, 1); Return (long) TimeSpan. totalseconds;//gets the 10-bit return (long) TimeSpan.
            TotalMilliseconds;
            catch (Exception) {return-1; The public static long Datetimetounixtimestamp () {var start = new DateTime (1970, 1,
            1, 0, 0, 0, DateTime.Now.Kind); Return Convert.toint64 ((Datetime.now-start).
        TotalSeconds); public static long Getcurrenttimeunix () {TimeSpan chA = (Datetime.now-timezone.currenttimezone.tolocaltime (New System.DateTime (1970, 1, 1)); Long T = (long) cha.
            TotalSeconds;
        return t; }

Run Results:

When you see the results, you'll find seconds. That's a little different, probably the difference between Beijing time and standard Time zone, where there is a mistake, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.