C#

推荐列表 站点导航

当前位置:首页 > 脚本编程 > C# >

C#_timespan使用方法详解,几点重要的用法:a 先来介绍

来源:网络整理  作者:  发布时间:2020-12-19 14:25
timespan使用方法详解,几点重要的用法:a 先来介绍几个方法TimeSpan.Minutes(其他时间比如天数,小时数,秒数都一样的情...
string tsMin=ts1.Minutes.ToString();
    }
 

TimeSpan ts11=new TimeSpan(t1.Tick);
        Console.WriteLine(TimeSpanUtility.FormatString(span));  // Output: 12d 23h 24m 2s
        if (string.IsNullOrEmpty(unit)) return string.Empty;



    }

string time1 = "2010-5-26 8:10:00";
 得到一个 TimeSpan 实例,TimeSpan 有一些属性:Days、TotalDays、Hours、TotalHours、Minutes、TotalMinutes、Seconds、TotalSeconds、Ticks,注意没有 TotalTicks。

        {
    {

for (int i = 0; i < timeParts.Length; i++)

Console.WriteLine(TimeSpanUtility.TimeSpanInWords(span));   // Output: 12 days, 23 hours, 24 minutes, 2 seconds
diff:就表示两个时间相差的分钟数,上面的例子就是610分钟。

时间 1 是 2010-1-2 8:43:35;
using System;
用时间 2 减时间 1,得到一个 TimeSpan 实例。

int[] timeParts = new[] { aTimeSpan.Days, aTimeSpan.Hours, aTimeSpan.Minutes, aTimeSpan.Seconds };

class TimeSpanUtility
    {

举例说明

时间 2 是 2010-1-12 8:43:34。
Subtract:表示两个时间段的差

{

DateTime.Tick :是一个计时周期,表示一百纳秒,即一千万分之一秒,那么 Ticks 在这里表示总共相差多少个时间周期,即:9 * 24 *                    3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000。3600 是一小时                    的秒数
TimeSpan ts1=t2-t1;
        string[] timeUnits = new[] { "day", "hour", "minute", "second" };


    public static string FormatString(TimeSpan aTimeSpan)
TimeSpan.Minutes(其他时间比如天数,小时数,秒数都一样的情况下得到的分钟数的差),其他的Hours,Second一样

n = Math.Abs(n); // -1 should be singular, too

所以以后想知道两个时间段的差就容易的多了

            {
string diff=ts22.Subtract(ts11).TotalMinutes.ToString();
string time2 = "2010-5-26 18:20:00";
    static void Main()
TimeSpan ts22=new TimeSpan(t2.Tick);

return timeStrings.Count != 0 ? string.Join(", ", timeStrings.ToArray()) : "0 seconds";
        // 1d 3h 43m 23s

    {
            if (timeParts[i] > 0)


            }
        }

复制代码 代码如下:

DateTime t2 = Convert.ToDateTime(time2);

return newFormat;
        List<string> timeStrings = new List<string>();

复制代码 代码如下:

}
        TimeSpan span = new TimeSpan(12, 23, 24, 2);

TimeSpan Format Helper

        // 12 days, 23 hours, 24 minutes, 2 seconds.
这些属性名称开始理解有些困难,但阅读本文后,相应您一定茅塞顿开。

b 两个时间的差

public class Client
DateTime t1 = Convert.ToDateTime(time1);

public static string TimeSpanInWords(TimeSpan aTimeSpan)
        string newFormat = aTimeSpan.ToString("d'd 'h'h 'm'm 's's'");

using System.Collections.Generic;

{
                timeStrings.Add(string.Format("{0} {1}", timeParts[i], Pluralize(timeParts[i], timeUnits[i])));
TimeSpan.TotalDays:两个时间段相差的日数,其他的TotalHours,TotalMinutes,TotalSeconds 一样

几点重要的用法:
    }

    {

那么,Days 就是 9,Hours 就是 23,Minutes 就是 59,Seconds 就是 59。

}


a 先来介绍几个方法

那么时间 2 比时间 1 多 9 天 23 小时 59 分 59 秒。

    }

return unit + (n == 1 ? string.Empty : "s");

private static string Pluralize(int n, string unit)


相关热词: 详解 方法 C#

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/c/5641.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

C#_timespan使用方法详解,几点重要的用法:a 先来介绍

2020-12-19 编辑:

string tsMin=ts1.Minutes.ToString();
    }
 

TimeSpan ts11=new TimeSpan(t1.Tick);
        Console.WriteLine(TimeSpanUtility.FormatString(span));  // Output: 12d 23h 24m 2s
        if (string.IsNullOrEmpty(unit)) return string.Empty;



    }

string time1 = "2010-5-26 8:10:00";
 得到一个 TimeSpan 实例,TimeSpan 有一些属性:Days、TotalDays、Hours、TotalHours、Minutes、TotalMinutes、Seconds、TotalSeconds、Ticks,注意没有 TotalTicks。

        {
    {

for (int i = 0; i < timeParts.Length; i++)

Console.WriteLine(TimeSpanUtility.TimeSpanInWords(span));   // Output: 12 days, 23 hours, 24 minutes, 2 seconds
diff:就表示两个时间相差的分钟数,上面的例子就是610分钟。

时间 1 是 2010-1-2 8:43:35;
using System;
用时间 2 减时间 1,得到一个 TimeSpan 实例。

int[] timeParts = new[] { aTimeSpan.Days, aTimeSpan.Hours, aTimeSpan.Minutes, aTimeSpan.Seconds };

class TimeSpanUtility
    {

举例说明

时间 2 是 2010-1-12 8:43:34。
Subtract:表示两个时间段的差

{

DateTime.Tick :是一个计时周期,表示一百纳秒,即一千万分之一秒,那么 Ticks 在这里表示总共相差多少个时间周期,即:9 * 24 *                    3600 * 10000000 + 23 * 3600 * 10000000 + 59 * 60 * 10000000 + 59 * 10000000 = 8639990000000。3600 是一小时                    的秒数
TimeSpan ts1=t2-t1;
        string[] timeUnits = new[] { "day", "hour", "minute", "second" };


    public static string FormatString(TimeSpan aTimeSpan)
TimeSpan.Minutes(其他时间比如天数,小时数,秒数都一样的情况下得到的分钟数的差),其他的Hours,Second一样

n = Math.Abs(n); // -1 should be singular, too

所以以后想知道两个时间段的差就容易的多了

            {
string diff=ts22.Subtract(ts11).TotalMinutes.ToString();
string time2 = "2010-5-26 18:20:00";
    static void Main()
TimeSpan ts22=new TimeSpan(t2.Tick);

return timeStrings.Count != 0 ? string.Join(", ", timeStrings.ToArray()) : "0 seconds";
        // 1d 3h 43m 23s

    {
            if (timeParts[i] > 0)


            }
        }

复制代码 代码如下:

DateTime t2 = Convert.ToDateTime(time2);

return newFormat;
        List<string> timeStrings = new List<string>();

复制代码 代码如下:

}
        TimeSpan span = new TimeSpan(12, 23, 24, 2);

TimeSpan Format Helper

        // 12 days, 23 hours, 24 minutes, 2 seconds.
这些属性名称开始理解有些困难,但阅读本文后,相应您一定茅塞顿开。

b 两个时间的差

public class Client
DateTime t1 = Convert.ToDateTime(time1);

public static string TimeSpanInWords(TimeSpan aTimeSpan)
        string newFormat = aTimeSpan.ToString("d'd 'h'h 'm'm 's's'");

using System.Collections.Generic;

{
                timeStrings.Add(string.Format("{0} {1}", timeParts[i], Pluralize(timeParts[i], timeUnits[i])));
TimeSpan.TotalDays:两个时间段相差的日数,其他的TotalHours,TotalMinutes,TotalSeconds 一样

几点重要的用法:
    }

    {

那么,Days 就是 9,Hours 就是 23,Minutes 就是 59,Seconds 就是 59。

}


a 先来介绍几个方法

那么时间 2 比时间 1 多 9 天 23 小时 59 分 59 秒。

    }

return unit + (n == 1 ? string.Empty : "s");

private static string Pluralize(int n, string unit)


本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/c/5641.shtml

风云图片