c#循环两个日期之前的时间
Posted 江小白ra
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#循环两个日期之前的时间相关的知识,希望对你有一定的参考价值。
1 for (DateTime dt = startDate; dt < endDate; dt = dt.AddMonths(1)) 2 { 3 var year = date.Year; 4 var month= date.Month; 5 }
public static class DateTimeUtil { /// <summary> /// 时间戳计时开始时间 /// </summary> private static DateTime timeStampStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); /// <summary> /// DateTime转换为10位时间戳(单位:秒) /// </summary> /// <param name="dateTime"> DateTime</param> /// <returns>10位时间戳(单位:秒)</returns> public static long DateTimeToTimeStamp(DateTime dateTime) { return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalSeconds; } /// <summary> /// DateTime转换为13位时间戳(单位:毫秒) /// </summary> /// <param name="dateTime"> DateTime</param> /// <returns>13位时间戳(单位:毫秒)</returns> public static long DateTimeToLongTimeStamp(DateTime dateTime) { return (long)(dateTime.ToUniversalTime() - timeStampStartTime).TotalMilliseconds; } /// <summary> /// 10位时间戳(单位:秒)转换为DateTime /// </summary> /// <param name="timeStamp">10位时间戳(单位:秒)</param> /// <returns>DateTime</returns> public static DateTime TimeStampToDateTime(long timeStamp) { return timeStampStartTime.AddSeconds(timeStamp).ToLocalTime(); } /// <summary> /// 13位时间戳(单位:毫秒)转换为DateTime /// </summary> /// <param name="longTimeStamp">13位时间戳(单位:毫秒)</param> /// <returns>DateTime</returns> public static DateTime LongTimeStampToDateTime(long longTimeStamp) { return timeStampStartTime.AddMilliseconds(longTimeStamp).ToLocalTime(); } } public static partial class Ext { /// <summary> /// 时间工具类 /// </summary> /// <summary> /// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss" /// </summary> /// <param name="dateTime">日期</param> /// <param name="isRemoveSecond">是否移除秒</param> public static string ToDateTimeString(this DateTime dateTime, bool isRemoveSecond = false) { if (isRemoveSecond) return dateTime.ToString("yyyy-MM-dd HH:mm"); return dateTime.ToString("yyyy-MM-dd HH:mm:ss"); } /// <summary> /// 获取格式化字符串,带时分秒,格式:"yyyy-MM-dd HH:mm:ss" /// </summary> /// <param name="dateTime">日期</param> /// <param name="isRemoveSecond">是否移除秒</param> public static string ToDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false) { if (dateTime == null) return string.Empty; return ToDateTimeString(dateTime.Value, isRemoveSecond); } /// <summary> /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd" /// </summary> /// <param name="dateTime">日期</param> public static string ToDateString(this DateTime dateTime) { return dateTime.ToString("yyyy-MM-dd"); } /// <summary> /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd" /// </summary> /// <param name="dateTime">日期</param> public static string ToDateString() { return DateTime.Now.ToString("yyyy-MM-dd"); } /// <summary> /// 获取格式化字符串,不带时分秒,格式:"yyyy-MM-dd" /// </summary> /// <param name="dateTime">日期</param> public static string ToDateString(this DateTime? dateTime) { if (dateTime == null) return string.Empty; return ToDateString(dateTime.Value); } /// <summary> /// 获取格式化字符串,不带年月日,格式:"HH:mm:ss" /// </summary> /// <param name="dateTime">日期</param> public static string ToTimeString(this DateTime dateTime) { return dateTime.ToString("HH:mm:ss"); } /// <summary> /// 获取格式化字符串,不带年月日,格式:"HH:mm:ss" /// </summary> /// <param name="dateTime">日期</param> public static string ToTimeString(this DateTime? dateTime) { if (dateTime == null) return string.Empty; return ToTimeString(dateTime.Value); } /// <summary> /// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff" /// </summary> /// <param name="dateTime">日期</param> public static string ToMillisecondString(this DateTime dateTime) { return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); } /// <summary> /// 获取格式化字符串,带毫秒,格式:"yyyy-MM-dd HH:mm:ss.fff" /// </summary> /// <param name="dateTime">日期</param> public static string ToMillisecondString(this DateTime? dateTime) { if (dateTime == null) return string.Empty; return ToMillisecondString(dateTime.Value); } /// <summary> /// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日" /// </summary> /// <param name="dateTime">日期</param> public static string ToChineseDateString(this DateTime dateTime) { return string.Format("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day); } /// <summary> /// 获取格式化字符串,不带时分秒,格式:"yyyy年MM月dd日" /// </summary> /// <param name="dateTime">日期</param> public static string ToChineseDateString(this DateTime? dateTime) { if (dateTime == null) return string.Empty; return ToChineseDateString(dateTime.SafeValue()); } /// <summary> /// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分" /// </summary> /// <param name="dateTime">日期</param> /// <param name="isRemoveSecond">是否移除秒</param> public static string ToChineseDateTimeString(this DateTime dateTime, bool isRemoveSecond = false) { StringBuilder result = new StringBuilder(); result.AppendFormat("{0}年{1}月{2}日", dateTime.Year, dateTime.Month, dateTime.Day); result.AppendFormat(" {0}时{1}分", dateTime.Hour, dateTime.Minute); if (isRemoveSecond == false) result.AppendFormat("{0}秒", dateTime.Second); return result.ToString(); } /// <summary> /// 获取格式化字符串,带时分秒,格式:"yyyy年MM月dd日 HH时mm分" /// </summary> /// <param name="dateTime">日期</param> /// <param name="isRemoveSecond">是否移除秒</param> public static string ToChineseDateTimeString(this DateTime? dateTime, bool isRemoveSecond = false) { if (dateTime == null) return string.Empty; return ToChineseDateTimeString(dateTime.Value); } }
对json的一些操作
1 public static class Json 2 { 3 public static object ToJson(this string Json) 4 { 5 return Json == null ? null : JsonConvert.DeserializeObject(Json); 6 } 7 public static string ToJson(this object obj) 8 { 9 var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }; 10 return JsonConvert.SerializeObject(obj, timeConverter); 11 } 12 public static string ToJson(this object obj, string datetimeformats) 13 { 14 var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats }; 15 return JsonConvert.SerializeObject(obj, timeConverter); 16 } 17 public static T ToObject<T>(this string Json) 18 { 19 return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json); 20 } 21 public static List<T> ToList<T>(this string Json) 22 { 23 return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json); 24 } 25 public static DataTable ToTable(this string Json) 26 { 27 return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json); 28 } 29 public static JObject ToJObject(this string Json) 30 { 31 return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", "")); 32 } 33 }
以上是关于c#循环两个日期之前的时间的主要内容,如果未能解决你的问题,请参考以下文章
Unity HTFramework框架(四十)Debug的性能监控
如何在 C# 中使用 csvHelper 将两个单独列中的日期和时间合并到一个新的日期时间列中