时间差、时间戳和日期格式化

//时间差
 TimeSpan span = model.EndtTime.Subtract(model.StartTime);
                if (Convert.ToInt32(span.TotalSeconds) > 7200)
                {
                    msgResultModel.Msg = "查询时间跨度不能超过2小时";
                    return msgResultModel;
                }

//时间戳
 TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                string timeSpan = Convert.ToInt64(ts.TotalSeconds).ToString();

//日期格式化
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"));
 
Console.WriteLine(DateTime.Now.ToString("d"));                    //格式: 2010-10-22
Console.WriteLine(DateTime.Now.ToString("D"));                    //格式: 2010年10月22日
 
Console.WriteLine(DateTime.Now.ToString("f"));                    //格式: 2010年10月22日 9:26
Console.WriteLine(DateTime.Now.ToString("F"));                    //格式: 2010年10月22日 9:26:38
 
Console.WriteLine(DateTime.Now.ToString("g"));                    //格式: 2010-10-22 9:26
Console.WriteLine(DateTime.Now.ToString("G"));                    //格式: 2010-10-22 9:26:38
 
Console.WriteLine(DateTime.Now.ToString("m"));                    //格式: 10月22日
Console.WriteLine(DateTime.Now.ToString("r"));                    //格式: Fri, 22 Oct 2010 09:26:38 GMT
Console.WriteLine(DateTime.Now.ToString("s"));                    //格式: 2010-10-22T09:26:38
 
Console.WriteLine(DateTime.Now.ToString("t"));                    //格式: 9:26
Console.WriteLine(DateTime.Now.ToString("T"));                    //格式: 9:26:38
 
Console.WriteLine(DateTime.Now.ToString("u"));                    //格式: 2010-10-22 09:26:38Z
Console.WriteLine(DateTime.Now.ToString("U"));                    //格式: 2010年10月22日 1:26:38
 
Console.WriteLine(DateTime.Now.ToString("y"));                    //格式: 2010年10月
Console.WriteLine(DateTime.Now.ToString("dddd"));                 //格式: 星期五
Console.WriteLine(DateTime.Now.ToString("dddd, MMMM dd yyyy"));   //格式: 星期五, 十月 22 2010
Console.WriteLine(DateTime.Now.ToString("ddd, MMM d yy"));        //格式: 五, 十月 22 10
Console.WriteLine(DateTime.Now.ToString("dddd, MMMM dd"));        //格式: 星期五, 十月 22
Console.WriteLine(DateTime.Now.ToString("M/yy"));                 //格式: 10-10

  

posted @ 2022-02-08 09:28  飞鱼上树了  阅读(38)  评论(0编辑  收藏  举报
/* 看板娘 */