zoukankan      html  css  js  c++  java
  • 时间转换:DateTimeExtensions

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Beehive.CloudReader.Utility
    {
        public static class DateTimeExtensions
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dateTime"></param>
            /// <returns></returns>
            public static string ToRelativeDateTimeT(this DateTime dateTime)
            {
                var time = DateTime.Now - dateTime;
    
                //如果不在同一年 则直接显示年月日
                if ((int.Parse(dateTime.ToString("yyyy")) - int.Parse(DateTime.Now.ToString("yyyy"))) != 0) // if (time.TotalDays > 365)
                    return dateTime.ToString("yyyy-MM-dd");
                if (time.TotalDays > 2)
                    return dateTime.ToString("M月d日 HH:mm");
                else
                {
                    var day = DateTime.Now.Date - dateTime.Date;
                    if (day.Days == 2)
                        return string.Format("前天{0:HH:mm}", dateTime);
                    if (day.Days == 1)
                        return string.Format("昨天{0:HH:mm}", dateTime);
                    if (time.TotalMinutes > 60)
                        return string.Format("今天{0:HH:mm}", dateTime);
                    if (time.TotalSeconds > 60)
                        return string.Format("{0}分钟前", time.Minutes);
                    if (time.TotalSeconds < 60 && time.TotalDays >= 0)
                        return "刚刚";
                    else
                    {
                        if (time.Days == 0 && dateTime.Date == DateTime.Now.Date)
                            return string.Format("今天{0:HH:mm}", dateTime);
                        if (time.TotalDays >= -1)
                            return string.Format("明天{0:HH:mm}", dateTime);
                        if (time.TotalDays < -1 && time.TotalDays > -2)
                            return string.Format("后天{0:HH:mm}", dateTime);
                        return dateTime.ToString("M月d日 HH:mm");
                    }
                }
            }
    
    
            public static string ToRelativeDateTimeDay(this DateTime dateTime)
            {
                var time = DateTime.Now - dateTime;
    
                //如果不在同一年 则直接显示年月日
                if ((int.Parse(dateTime.ToString("yyyy")) - int.Parse(DateTime.Now.ToString("yyyy"))) != 0) // if (time.TotalDays > 365)
                    return dateTime.ToString("yyyy-MM-dd");
                if (time.TotalDays > 2)
                    return dateTime.ToString("M月d日");
                else
                {
                    var day = DateTime.Now.Date - dateTime.Date;
                    if (day.Days == 2)
                        return string.Format("前天", dateTime);
                    if (day.Days == 1)
                        return string.Format("昨天", dateTime);
                    if (time.TotalMinutes > 60)
                        return string.Format("{0:HH:mm}", dateTime);
                    if (time.TotalSeconds > 60)
                        return string.Format("{0}分钟前", time.Minutes);
                    if (time.TotalSeconds < 60 && time.TotalDays >= 0)
                        return "刚刚";
                    else
                    {
                        if (time.Days == 0 && dateTime.Date == DateTime.Now.Date)
                            return string.Format("{0:HH:mm}", dateTime);
                        if (time.TotalDays >= -1)
                            return string.Format("明天", dateTime);
                        if (time.TotalDays < -1 && time.TotalDays > -2)
                            return string.Format("后天", dateTime);
                        return dateTime.ToString("M月d日");
                    }
                }
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dateTime"></param>
            /// <returns></returns>
            public static long ToTimeStamp(this DateTime dateTime)
            {
                var stamp = dateTime.Ticks - new DateTime(1970, 1, 1).Ticks;
                return stamp / 10000000 - 8 * 60 * 60;
            }
    
    
            public static string ToWelcome(this DateTime dateTime)
            {
                var hour = DateTime.Now.Hour;
                var msg = "";
                if (hour >= 6 && hour < 8)
                {
                    msg = "早上好";
                }
                else if (hour >= 8 && hour < 11)
                {
                    msg = "上午好";
                }
                else if (hour >= 11 && hour < 13)
                {
                    msg = "中午好";
                }
                else if (hour >= 13 && hour < 18)
                {
                    msg = "下午好";
                }
                else
                {
                    msg = "晚上好";
                }
                return msg;
            }
        }
    }
  • 相关阅读:
    前端插件之Datatables使用--上篇
    Django websocket之web端实时查看日志实践案例
    前端插件之Select2使用
    前端插件之Bootstrap Dual Listbox使用
    简单易落地且对运维友好的配置中心方案
    Devops-运维效率之数据迁移自动化
    中小团队基于Docker的Devops实践
    nginx第七篇:Nginx集群(负载均衡)、集群介绍、构建集群、nginx默认分发算法、nginx基于请求头的分发算法
    nginx之旅(第六篇):nginx优化--nginx优化目的、工作进程优化、长连接设置、数据压缩、客户端缓存
    nginx之旅(第五篇):URL重写介绍、URL重写场景、URL重写语法
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3543819.html
Copyright © 2011-2022 走看看