下面介绍C#生成日期与时间的方法
主要涉及到2个类,DateTime 和 TimeSpan
//获取当前时间 DateTime now = DateTime.Now; int year = now.Year; int month = now.Month; int day = now.Day; int hour = now.Hour; //DateTime 转换 string string dateTime1 = now.ToString("yyyy-MM-dd HH:mm:ss"); //string 转换 DateTime DateTime dateTime2 = DateTime.ParseExact("2021/10/30 18:02:00", "yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture); //时间戳生成 TimeSpan universal = DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)); //秒级 long seconds = (long)universal.TotalSeconds; //毫秒级 long milli = (long)universal.TotalMilliseconds;