php timespan,timespan使用方法详解

php timespan,timespan使用方法详解usingSystem usingSystem Collections Generic classTimeSpa publicstatic TimeSpanaTim stringnewFor aTimeSpan ToString d d h h m m s s 1d3h43m23sr

using System;

using System.Collections.Generic;

class TimeSpanUtility

{

public static string FormatString(TimeSpan aTimeSpan)

{

string newFormat = aTimeSpan.ToString(“d’d ‘h’h ‘m’m ‘s’s'”);

// 1d 3h 43m 23s

return newFormat;

}

public static string TimeSpanInWords(TimeSpan aTimeSpan)

{

List timeStrings = new List();

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

string[] timeUnits = new[] { “day”, “hour”, “minute”, “second” };

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

{

if (timeParts[i] > 0)

{

timeStrings.Add(string.Format(“{0} {1}”, timeParts[i], Pluralize(timeParts[i], timeUnits[i])));

}

}

return timeStrings.Count != 0 ? string.Join(“, “, timeStrings.ToArray()) : “0 seconds”;

}

private static string Pluralize(int n, string unit)

{

if (string.IsNullOrEmpty(unit)) return string.Empty;

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

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

}

}

public class Client

{

static void Main()

{

// 12 days, 23 hours, 24 minutes, 2 seconds.

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

Console.WriteLine(TimeSpanUtility.TimeSpanInWords(span));   // Output: 12 days, 23 hours, 24 minutes, 2 seconds

Console.WriteLine(TimeSpanUtility.FormatString(span));  // Output: 12d 23h 24m 2s

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215325.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午2:10
下一篇 2026年3月18日 下午2:10


相关推荐

  • 视频采用的压缩编码_视频压缩编码存在的冗余信息

    视频采用的压缩编码_视频压缩编码存在的冗余信息1、什么是H.261编码协议        答:H.261是最早出现的视频编码建议,它采用的算法结合了可减少时间冗余的帧间预测和可减少空间冗余的DCT变换的混合编码方法,其输出码率是p×64kbit/s。p取值较小时,只能传清晰度不太高的图像,适合于面对面的电视电话;p取值较大时(如p>6),可以传输清晰度较好的会议电视图像。该标准主要针对ISDN电话线的视频会议,可视电话等,ISDN的

    2025年6月23日
    4
  • php递归函数详解_用php递归函数实现阶乘计算

    php递归函数详解_用php递归函数实现阶乘计算本节内容:PHP递归算法。PHP递归算法代码:复制代码代码示例://定义PI一分的角度的值define(“PII”,M_PI/180);//新建图像资源,并定义其背景为白色,前景色为黑色$im=imagecreate(670,500);$white=imagecolorallocate($im,0xFF,0xFF,0xFF);$g=imagecolorallocate($im,0x00,0x0…

    2022年8月11日
    7
  • pycharm连接不上mysql中的数据库时_python Mysql时间带t

    pycharm连接不上mysql中的数据库时_python Mysql时间带t在pycharm连接mysql数据库时候,会出现时区错误的情况。默认都是讲时区改成‘+8:00’就好了。修改方法打开mysqlsetglobaltime_zone=’+8:00’但是,第二天再打开时,又出现报错,如图所示为了永久解决。可以再my.ini文件中最后加上,setglobaltime_zone=’+8:00’。my.ini默认在C:\ProgramData\MySQL\MySQLServer8.0修改my.ini成功解决后患…

    2022年8月26日
    10
  • 月之暗面Kimi发布新模型Kimina

    月之暗面Kimi发布新模型Kimina

    2026年3月12日
    2
  • 腾讯元宝,重大更新!

    腾讯元宝,重大更新!

    2026年3月13日
    4
  • Map集合的遍历[通俗易懂]

    Map集合的遍历[通俗易懂]COPY/***Map接口的使用*特点:1.存储键值对2.键不能重复,值可以重复3.无序*/publicclassDemo1{ publicstaticvoidmain(String[]args){ Map<String,Integer>map=newHashMap<String,Integer>(); //1.添加元素 map.put(“tang”,21); map.put(“he”,22); map.put(“

    2022年5月7日
    44

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号