`
highfly-s
  • 浏览: 96802 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

时间的计算

 
阅读更多

using System;

 

namespace Beisen.TalentPortal.Infrastructure.Helper

{

    public static class DateTimeExtensions

    {

        public static int MonthsBetweentwoDates(DateTime startTime,DateTime endTime)

        {

            int yearNum = (endTime.Year - startTime.Year) * 12;

 

            int monthNum = endTime.Month - startTime.Month;

 

            int dayNum = endTime.Day - startTime.Day > 0 ? 1 : 0;

 

            return yearNum + monthNum + dayNum;

        }

 

        public static string FormatDate(this DateTime dateTime)

        {

            var now = DateTime.Now;

 

            if (now.Year == dateTime.Year)

            {

                if (now.Date == dateTime.Date)

                {

                    TimeSpan time = now - dateTime;

                    if (time.Hours < 24 && time.Hours >= 1)

                    {

                        return string.Format("{0}小时前", time.Hours);

                    }

                    else if (time.Minutes < 60 && time.Minutes >= 1)

                    {

                        return string.Format("{0}分钟前", time.Minutes);

                    }

                    else if (time.Seconds < 60 && time.Seconds > 0)

                    {

                        return string.Format("{0}秒前", time.Seconds);

                    }

                    else

                    {

                        return "刚刚";

                    }

                }

                else if (now.Date == dateTime.Date.AddDays(1))

                {

                    return string.Format("昨天 {0}", dateTime.ToString("HH:mm"));

                }

                else if (now.Date == dateTime.Date.AddDays(2))

                {

                    return string.Format("前天 {0}", dateTime.ToString("HH:mm"));

                }

                else

                {

                    return string.Format("{0}月{1}日 {2}", dateTime.Month, dateTime.Day, dateTime.ToString("HH:mm"));

                }

            }

            else

            {

                return string.Format("{3}年{0}月{1}日 {2}", dateTime.Month, dateTime.Day, dateTime.ToString("HH:mm"), dateTime.Year);

            }

            return string.Empty;

        }

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatDateCn(this DateTime dateTime)

        {

            return dateTime.ToString("yyyy/MM/dd HH:mm:ss");

        }

 

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatDateCn(this string dateTime)

        {

            if (string.IsNullOrWhiteSpace(dateTime))

                return null;

 

            DateTime time;

            DateTime.TryParse(dateTime, out time);

 

            return time.ToString("yyyy/MM/dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);

        }

 

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatDateCn(this DateTime? dateTime)

        {

            if (dateTime == null || !dateTime.HasValue)

                return "";

 

            return dateTime.Value.ToString("yyyy/MM/dd HH:mm:ss");

        }

 

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatShortDate(this DateTime dateTime)

        {

            return dateTime.ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);

        }

 

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatShortDate(this string dateTime)

        {

            if (string.IsNullOrWhiteSpace(dateTime))

                return null;

 

            DateTime time;

            DateTime.TryParse(dateTime, out time);

 

            return time.ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);

        }

 

        /// <summary>

        /// 转换日期为指定的格式 yyyy/MM/dd HH:mm:ss

        /// </summary>

        public static string FormatShortDateCn(this DateTime? dateTime)

        {

            if (dateTime == null || !dateTime.HasValue)

                return "";

 

            return dateTime.Value.ToString("yyyy/MM/dd");

        }

 

        /// <summary>

        /// 所在周的起止时间

        /// </summary>

        /// <param name="dateTime"></param>

        /// <returns></returns>

        public static DateTime[] DatesIncludeDay(this DateTime dateTime)

        {

            DateTime[] dts = new DateTime[2];

            int dayofweek = DayOfWeek(dateTime);

            if(dayofweek==0)

            {

                dts[0] = dateTime.AddDays(-7);

                dts[1] = dateTime.AddDays(6);

            }

            else

            {

                dts[0] = dateTime.AddDays(-dayofweek);

                dts[1] = dateTime.AddDays(6 - dayofweek);

            }

         

            return dts;

        }

        /// <summary>

        /// 计算星期几,转换为数字

        /// </summary>

        /// <param name="dt">某天的日期</param>

        /// <returns></returns>

        private static int DayOfWeek(DateTime dt)

        {

            string strDayOfWeek = dt.DayOfWeek.ToString().ToLower();

            int intDayOfWeek = 0;

            switch (strDayOfWeek)

            {

                case "monday":

                    intDayOfWeek = 1;

                    break;

                case "tuesday":

                    intDayOfWeek = 2;

                    break;

                case "wednesday":

                    intDayOfWeek = 3;

                    break;

                case "thursday":

                    intDayOfWeek = 4;

                    break;

                case "friday":

                    intDayOfWeek = 5;

                    break;

                case "saturday":

                    intDayOfWeek = 6;

                    break;

                case "sunday":

                    intDayOfWeek = 0;

                    break;

            }

            return intDayOfWeek;

        }

 

        /// <summary>

        /// 计算星期几,转换为汉字

        /// </summary>

        /// <param name="dt">某天的日期</param>

        /// <returns></returns>

        public static string DayOfChWeek(this DateTime dt)

        {

            var strDayOfWeek = dt.DayOfWeek;

            switch ((int)strDayOfWeek)

            {

                case 1:

                    return "一";

                case 2:

                    return "二";

                case 3:

                    return "三";

                case 4:

                    return "四";

                case 5:

                    return "五";

                case 6:

                    return "六";

                case 0:

                    return "日";

            }

            return string.Empty;

        }

        /// <summary>  

        /// DateTime时间格式转换为Unix时间戳格式  

        /// </summary>  

        /// <param name="time"> DateTime时间格式</param>  

        /// <returns>Unix时间戳格式</returns>  

        public static int ConvertDateTimeInt(this DateTime time)

        {

            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

            return (int)(time - startTime).TotalSeconds;

        }  

    }

}

以上是用的后台程序进行的计算,后来发现了一个更好用的前台JS 插件:moment

网址:http://momentjs.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics