posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
DatetimeUtil
复制代码
/// <summary>
/// DateTime转换为10位时间戳(单位:秒)
/// </summary>
/// <param name="dateTime"> DateTime</param>
/// <returns>10位时间戳(单位:秒)</returns>
public static long DateTimeToTimeStamp(DateTime dateTime)
{
    return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
/// <summary>
/// DateTime转换为13位时间戳(单位:毫秒)
/// </summary>
/// <param name="dateTime"> DateTime</param>
/// <returns>13位时间戳(单位:毫秒)</returns>
public static long DateTimeToLongTimeStamp(DateTime dateTime)
{
    return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
}
/// <summary>
/// 10位时间戳(单位:秒)转换为DateTime
/// </summary>
/// <param name="timeStamp">10位时间戳(单位:秒)</param>
/// <returns>DateTime</returns>
public static DateTime TimeStampToDateTime(long timeStamp)
{
    return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timeStamp).ToLocalTime();
}
/// <summary>
/// 13位时间戳(单位:毫秒)转换为DateTime
/// </summary>
/// <param name="longTimeStamp">13位时间戳(单位:毫秒)</param>
/// <returns>DateTime</returns>
public static DateTime LongTimeStampToDateTime(long longTimeStamp)
{
    return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(longTimeStamp).ToLocalTime();
}
复制代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/// <summary>
        /// 本时区日期时间转时间戳
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns>long=Int64</returns>
        public static long DateTimeToTimestamp(DateTime datetime)
        {
            DateTime dd = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime timeUTC = DateTime.SpecifyKind(datetime, DateTimeKind.Utc);//本地时间转成UTC时间
            TimeSpan ts = (timeUTC - dd);
            return (Int64)ts.TotalMilliseconds;//精确到毫秒
        }
        /// <summary>
        /// 时间戳转本时区日期时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime TimestampToDateTime(string timeStamp)
        {
            DateTime dd = DateTime.SpecifyKind(new DateTime(1970, 1, 1, 0, 0, 0, 0), DateTimeKind.Local);
            long longTimeStamp = long.Parse(timeStamp + "0000");
            TimeSpan ts = new TimeSpan(longTimeStamp);
            return dd.Add(ts);
        }

C#时间戳与日期互转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <summary> 
        /// 时间戳转为C#格式时间 
        /// </summary> 
        /// <param name="timeStamp">Unix时间戳格式</param> 
        /// <returns>C#格式时间</returns> 
        public static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }
 
        /// <summary> 
        /// DateTime时间格式转换为Unix时间戳格式 
        /// </summary> 
        /// <param name="time"> DateTime时间格式</param> 
        /// <returns>Unix时间戳格式</returns> 
        public static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

  

使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/// <summary>
        /// 使用C#把发表的时间改为几个月,几天前,几小时前,几分钟前,或几秒前
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DateStringFromNow(DateTime dt)
        {
            TimeSpan span = DateTime.Now - dt;
            if (span.TotalDays > 60)
            {
                return dt.ToShortDateString();
            }
            else
            {
                if (span.TotalDays > 30)
                {
                    return
                    "1个月前";
                }
                else
                {
                    if (span.TotalDays > 14)
                    {
                        return
                        "2周前";
                    }
                    else
                    {
                        if (span.TotalDays > 7)
                        {
                            return
                            "1周前";
                        }
                        else
                        {
                            if (span.TotalDays > 1)
                            {
                                return
                                string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
                            }
                            else
                            {
                                if (span.TotalHours > 1)
                                {
                                    return
                                    string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
                                }
                                else
                                {
                                    if (span.TotalMinutes > 1)
                                    {
                                        return
                                        string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
                                    }
                                    else
                                    {
                                        if (span.TotalSeconds >= 1)
                                        {
                                            return
                                            string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
                                        }
                                        else
                                        {
                                            return
                                            "1秒前";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

  

C#中使用TimeSpan计算两个时间的差值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// <summary>
       /// C#中使用TimeSpan计算两个时间的差值
       /// 可以反加两个日期之间任何一个时间单位。
       /// </summary>
       /// <param name="DateTime1"></param>
       /// <param name="DateTime2"></param>
       /// <returns></returns>
       public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
       {
           string dateDiff = null;
           TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
           TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
           TimeSpan ts = ts1.Subtract(ts2).Duration();
           dateDiff = ts.Days.ToString() + "天" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds.ToString() + "秒";
           return dateDiff;
       }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*1.DateTime值类型代表了一个从公元0001年1月1日0点0分0秒到公元9999年12月31日23点59分59秒之间的具体日期时刻。因此,你可以用DateTime值类型来描述任何在想象范围之内的时间。一个DateTime值代表了一个具体的时刻
        2.TimeSpan值包含了许多属性与方法,用于访问或处理一个TimeSpan值
        下面的列表涵盖了其中的一部分:
        Add:与另一个TimeSpan值相加。
        Days:返回用天数计算的TimeSpan值。
        Duration:获取TimeSpan的绝对值。
        Hours:返回用小时计算的TimeSpan值
        Milliseconds:返回用毫秒计算的TimeSpan值。
        Minutes:返回用分钟计算的TimeSpan值。
        Negate:返回当前实例的相反数。
        Seconds:返回用秒计算的TimeSpan值。
        Subtract:从中减去另一个TimeSpan值。
        Ticks:返回TimeSpan值的tick数。
        TotalDays:返回TimeSpan值表示的天数。
        TotalHours:返回TimeSpan值表示的小时数。
        TotalMilliseconds:返回TimeSpan值表示的毫秒数。
        TotalMinutes:返回TimeSpan值表示的分钟数。
        TotalSeconds:返回TimeSpan值表示的秒数。
        */

  来源:http://www.cnblogs.com/summers/p/3225716.html

posted on   邢帅杰  阅读(957)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示