C# about time (getting time in a specific format and getting the current timestamp in multiple ways)

1. Get the current time and convert it to a specific format:

string tradeTime = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo);

write picture description here

Set the desired time format:

string tradeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);

write picture description here

string tradeTime = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒", DateTimeFormatInfo.InvariantInfo);

write picture description here

2. Obtain the current timestamp (the timestamp obtained by taking milliseconds is 13 digits, and if only seconds are obtained, the timestamp is 10 digits)

第一种方法
/// <summary>  
        /// 获取当前时间戳  
        /// </summary>  
        /// <param name="bflag">为真时获取10位时间戳,为假时获取13位时间戳.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;
        }

write picture description here

Generally, only one type is used in a project, either 10 or 13 bits, so the following code can also be used directly

public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);//ToUniversalTime()转换为标准时区的时间,去掉的话直接就用北京时间
            return (long)ts.TotalMilliseconds; //精确到毫秒
            //return (long)ts.TotalSeconds;//获取10位
        }

The only difference between the following methods is whether you need to get exceptions,

//生成unix格式时间
        public static long getUnix()
        {
            try
            {
                TimeSpan timespan = DateTime.UtcNow - new DateTime(1970, 1, 1);
                //return (long)timespan.TotalSeconds;//获取10位
                return (long)timespan.TotalMilliseconds;
            }
            catch (Exception)
            {
                return -1;
            }
        }

The first four complete methods and running results

public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);//ToUniversalTime()转换为标准时区的时间,去掉的话直接就用北京时间
            return (long)ts.TotalMilliseconds; //精确到毫秒
            //return (long)ts.TotalSeconds;//获取10位
        }

        //生成unix格式时间
        public static long getUnix()
        {
            try
            {
                TimeSpan timespan = DateTime.UtcNow - new DateTime(1970, 1, 1);
                //return (long)timespan.TotalSeconds;//获取10位
                return (long)timespan.TotalMilliseconds;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        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;
        }

Running results:
write picture description here
When you see the results, you will find that the second block is slightly different, probably the difference between Beijing time and the standard time zone. Please leave a message to correct any mistakes

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325535309&siteId=291194637