时间函数总结

时间函数总结 

 DateTime currentTime=new System.DateTime();
//取当前年月日时分秒     
            currentTime=System.DateTime.Now;
//取当前年    
            int 年=currentTime.Year;
//取当前月    
            int 月=currentTime.Month;
//取当前日    
            int 日=currentTime.Day;
//取当前时    
            int 时=currentTime.Hour;
//取当前分    
            int 分=currentTime.Minute;
//取当前秒    
            int 秒=currentTime.Second;
//取当前毫秒   
            int 毫秒=currentTime.Millisecond; //(变量可用中文)
//取中文日期显示--年月日时分  
            string strY=currentTime.ToString("f"); //不显示秒
//取中文日期显示_年月    
            string strYM=currentTime.ToString("y");
//取中文日期显示_月日   
            string strMD=currentTime.ToString("m");
//取当前年月日,格式为:2013-10-23   
            string strYMD=currentTime.ToString("d");
//取当前时分,格式为:15:54   
            string strT=currentTime.ToString("t");


            Console.WriteLine("格式");


            Console.WriteLine(DateTime.Now.ToString());//获取当前系统时间 完整的日期和时间
            Console.WriteLine(DateTime.Now.ToLongDateString());//只显示日期 xxxx年xx月xx日 ,一个是长日期
            Console.WriteLine(DateTime.Now.ToShortDateString());//只显示日期 xxxx-xx-xx 一个是短日期
            //今天    
            Console.WriteLine("今天");
            Console.WriteLine(DateTime.Now.Date.ToShortDateString());
            //昨天 的   
            Console.WriteLine("昨天");
            Console.WriteLine(DateTime.Now.AddDays(-1).ToShortDateString());
            //明天 的       
            Console.WriteLine("明天");
            Console.WriteLine(DateTime.Now.AddDays(1).ToShortDateString());


            //本周(注意这里的每一周是从周日始至周六止)
            Console.WriteLine("周日周六");
            Console.WriteLine(DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString());
            Console.WriteLine(DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString());


            //上周,上周就是本周再减去7天
            
            DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
            DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
            //下周    本周再加上7天
            DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
            DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
            //本月    本月的第一天是1号,最后一天就是下个月一号再减一天。
          //  DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
          //  DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值