记录下C#中一些日期转换的方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
DateTime nowtime = DateTime.Now;
Console.WriteLine("当前日期:{0}", nowtime);
DateTime now = DateTime.Now.Date; //当前日期,DateTime.Now.Date和DateTime.Today一样都是获取年月日
Console.WriteLine("今天:{0}", now);
DateTime yesterday = now.Date.AddDays(-1);
Console.WriteLine("昨天:{0}", yesterday); //默认为短日期,长时间
DateTime tomorrow = now.Date.AddDays(1); //now.Date获取此实例的日期部分,时分秒默认为0
Console.WriteLine("明天:{0}", tomorrow);
DateTime starttime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0); //本月初
Console.WriteLine("本月初:{0}", starttime.ToString());
DateTime endtime = starttime.AddMonths(1).AddSeconds(-1); //本月末
Console.WriteLine("本月末:{0}", endtime.ToString());
DateTime startmonth = starttime.AddMonths(-1); //上月初
Console.WriteLine("上月初:{0}", startmonth);
DateTime endmonth = starttime.AddSeconds(-1); //上月末
Console.WriteLine("上月末:{0}", endmonth);
Console.ReadLine();
}
}
}
输出结果: | |
个人订阅号: |
|