C# 之 DataTime常用方法

C# DataTime

参数format格式详细用法:
格式字符 关联属性/说明
d ShortDatePattern
D LongDatePattern
f 完整日期和时间(长日期和短时间)
F FullDateTimePattern(长日期和长时间)
g 常规(短日期和短时间)
G 常规(短日期和长时间)
m MMonthDayPattern
r RRFC1123Pattern
s 使用当地时间的SortableDateTimePattern(基于ISO8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern用于显示通用时间的格式
U 使用通用时间的完整日期和时间(长日期和长时间)
y YYearMonthPattern

当前时间个获取:

namespace DataTimeTest
{
    class Program
    {
        static void Main(string[] args)
        {
        	//HH:24小时 hh:12小时
            Console.WriteLine("yyyy-MM-dd HH:mm:ss:fff      " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            Console.WriteLine("yyyy-MM-dd HH:mm:ss:ms       " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"));
            Console.WriteLine("y        " + DateTime.Now.ToString("y"));                    
            Console.WriteLine("dddd     "+DateTime.Now.ToString("dddd"));                 
            Console.WriteLine("dddd, MMMM dd yyyy       " + DateTime.Now.ToString("dddd, MMMM dd yyyy"));   
            Console.WriteLine("ddd, MMM d yy        " + DateTime.Now.ToString("ddd, MMM d yy"));        
            Console.WriteLine("dddd, MMMM dd        " + DateTime.Now.ToString("dddd, MMMM dd"));        
            Console.WriteLine("M/yy     " + DateTime.Now.ToString("M/yy"));                 
            Console.WriteLine("dd-MM-yy     " + DateTime.Now.ToString("dd-MM-yy"));
            Console.WriteLine();

            Console.WriteLine("ToString('t'):       "+DateTime.Now.ToString("t")); 
            Console.WriteLine("ToString('d'):       "+DateTime.Now.ToString("d"));
            Console.WriteLine("ToString('D'):       "+DateTime.Now.ToString("D"));
            Console.WriteLine("ToString('f'):       "+DateTime.Now.ToString("f"));
            Console.WriteLine("ToString('F'):       "+DateTime.Now.ToString("F"));
            Console.WriteLine("ToString('G'):       "+DateTime.Now.ToString("G"));
            Console.WriteLine("ToString('t'):       "+DateTime.Now.ToString("t"));
            Console.WriteLine("ToString('T'):       "+DateTime.Now.ToString("T"));
            Console.WriteLine("ToString('U'):       "+DateTime.Now.ToString("U"));
            Console.WriteLine("ToString('u'):       "+DateTime.Now.ToString("u"));
            Console.WriteLine("ToString('m'):       "+DateTime.Now.ToString("m"));
            Console.WriteLine("ToString('M'):       "+DateTime.Now.ToString("M"));
            Console.WriteLine("ToString('r'):       "+DateTime.Now.ToString("r"));
            Console.WriteLine("ToString('R'):       "+DateTime.Now.ToString("R"));
            Console.WriteLine("ToString('y'):       "+DateTime.Now.ToString("y"));
            Console.WriteLine("ToString('Y'):       "+DateTime.Now.ToString("Y"));
            Console.WriteLine("ToString('o'):       "+DateTime.Now.ToString("o"));
            Console.WriteLine("ToString('O'):       "+DateTime.Now.ToString("O"));
            Console.WriteLine("ToString('s'):       "+DateTime.Now.ToString("s"));

            Console.ReadKey();
        }
    }
}

C# 之 DataTime常用方法_第1张图片


实用小方法

计算时间差
		//调用示例:
    	DateDiff(Convert.ToDateTime("2019/03/30 11:13:10"), Convert.ToDateTime("2019/04/01 12:24:11"));
    	DateDiff(Convert.ToDateTime("2019-03-30 11:13:10"), Convert.ToDateTime("2019-04-01 12:24:11"));
            
        /// 
        /// 计算两个日期的时间间隔
        /// 
        /// 第一个日期和时间
        /// 第二个日期和时间
        /// 返回间隔几天
        private static int DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;

            TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract(ts2).Duration();
            dateDiff = ts.Days.ToString() + "天"
                + ts.Hours.ToString() + "小时"
                + ts.Minutes.ToString() + "分钟"
                + ts.Seconds.ToString() + "秒";

            Console.WriteLine(dateDiff);

            //只返回需要的时间间隔
            return ts.Days;
        }

C# DateTime转换为Unix时间戳

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
System.Console.WriteLine(timeStamp);

Unix时间戳转换为C# DateTime

long unixTimeStamp = 1478162177;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime dt = startTime.AddSeconds(unixTimeStamp);
System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff"));

你可能感兴趣的:(ジ﹋★☆『,C,#...,』)