.net工具类——时间操作
.net工具类——时间操作
#region 时间操作 /// <summary> /// 获得当前时间的""yyyy-MM-dd HH:mm:ss:fffffff""格式字符串 /// </summary> public static string GetDateTimeMS() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff"); } /// <summary> /// 获得当前时间的""yyyy年MM月dd日 HH:mm:ss""格式字符串 /// </summary> public static string GetDateTimeU() { return string.Format("{0:U}", DateTime.Now); } /// <summary> /// 获得当前时间的""yyyy-MM-dd HH:mm:ss""格式字符串 /// </summary> public static string GetDateTime() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } /// <summary> /// 获得当前日期 /// </summary> public static string GetDate() { return DateTime.Now.ToString("yyyy-MM-dd"); } /// <summary> /// 获得中文当前日期 /// </summary> public static string GetChineseDate() { return DateTime.Now.ToString("yyyy月MM日dd"); } /// <summary> /// 获得当前时间(不含日期部分) /// </summary> public static string GetTime() { return DateTime.Now.ToString("HH:mm:ss"); } /// <summary> /// 获得当前小时 /// </summary> public static string GetHour() { return DateTime.Now.Hour.ToString("00"); } /// <summary> /// 获得当前天 /// </summary> public static string GetDay() { return DateTime.Now.Day.ToString("00"); } /// <summary> /// 获得当前月 /// </summary> public static string GetMonth() { return DateTime.Now.Month.ToString("00"); } /// <summary> /// 获得当前年 /// </summary> public static string GetYear() { return DateTime.Now.Year.ToString(); } /// <summary> /// 获得当前星期(数字) /// </summary> public static string GetDayOfWeek() { return ((int)DateTime.Now.DayOfWeek).ToString(); } //星期数组 private static string[] _weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; /// <summary> /// 获得当前星期(汉字) /// </summary> public static string GetWeek() { return _weekdays[(int)DateTime.Now.DayOfWeek]; } #endregion