两种日期格式 yyyyMMddHHmmss、yyyy-MM-dd HH:mm:ss
string date1 = DateTime.Now.ToString("yyyyMMddHHmmss");
string date2 = DateTime.Now.AddHours(1).ToString("yyyyMMddHHmmss");
string date3 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string date4 = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss");
//分钟数
int op1 = Convert.ToInt32((DateTime.ParseExact(date2, "yyyyMMddHHmmss", CultureInfo.CurrentCulture) - DateTime.ParseExact(date1, "yyyyMMddHHmmss", CultureInfo.CurrentCulture)).TotalMinutes);
int op2 = Convert.ToInt32((Convert.ToDateTime(date4) - Convert.ToDateTime(date3)).TotalMinutes);
秒、小时换成对应的
TotalSeconds、TotalHours
计算当天属于哪一周
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
DateTime today = DateTime.Today;
int weekOfYear = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(today, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
Console.WriteLine("今天是一年中的第 {0} 周。", weekOfYear);
}
}