C# 之 DataTime常用方法

C# DataTime

参数format格式详细用法:
格式字符关联属性/说明
dShortDatePattern
DLongDatePattern
f完整日期和时间(长日期和短时间)
FFullDateTimePattern(长日期和长时间)
g常规(短日期和短时间)
G常规(短日期和长时间)
mMMonthDayPattern
rRRFC1123Pattern
s使用当地时间的SortableDateTimePattern(基于ISO8601)
tShortTimePattern
TLongTimePattern
uUniversalSortableDateTimePattern用于显示通用时间的格式
U使用通用时间的完整日期和时间(长日期和长时间)
yYYearMonthPattern

当前时间个获取:

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

result


实用小方法

计算时间差
		//调用示例:
    	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"));
            
        /// <summary>
        /// 计算两个日期的时间间隔
        /// </summary>
        /// <param name="DateTime1">第一个日期和时间</param>
        /// <param name="DateTime2">第二个日期和时间</param>
        /// <returns>返回间隔几天</returns>
        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"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈言必行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值