惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
雷峰网
雷峰网
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
P
Proofpoint News Feed
The Cloudflare Blog
D
Docker
大猫的无限游戏
大猫的无限游戏

博客园 - 饭

This MySqlConnection is already in use. See https://fl.vu/mysql-conn-reuse WinForm使用Python.Net调用Yolov8将图像显示在pictureBox控件上. EPPlus操作透视表过滤数据 JS/html5 文字转语音 关于IIS部署.NET7的记录 nginx伪静态过滤静态文件 阿里云Nginx伪静态 记录一些壁纸/图片网站 js四舍五入. javascript四舍五入 真正四舍五入 不是四舍六入五成双 比较两个相同类型的实例是否相同, 支持忽略指定字段. entity framework 实现按照距离排序[转] 正padding负margin实现多列等高布局(转) 在iframe内页触发顶层页面body的blur事件 bootstrap 模态窗口 多重/多个弹窗滚动条补丁 2019年6月车型数据Access数据库+缩略图 更新于2019年6月5日. sql server 2012 分页/dapper/C#拼sql/免储存过程/简易 正则: 匹配除了某些单词之外的其他单词 按顺序动态加载js, 可控版本, 有回调 asp.net mvc json数据缓存 知识点关键词(记录一下)
TimeSpan格式化字符串格式(摘)
· 2018-04-15 · via 博客园 - 饭

一直在用DateTime, 却不常用TimeSpan , 今天突然用到了, 发现不知道咋做格式化...百度一下,找到了答案, 在这记录一下, 免得以后找花费时间

以下内容摘抄自 Microsoft Docs  原文地址: https://docs.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/ee372287(v=vs.95)

分别展示了ToString方法跟string.Format方法中的方法, 其中string.Format的用法可以在mvc的Html.TextBox的format参数中使用

这里只记录下基本用法, 更多使用参考请移步上方链接.

TimeSpan转字符串

using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = null;
      output = "Time of Travel: " + duration.ToString("%d") + " days";
      outputBlock.Text += output + Environment.NewLine;
      output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss"); 
      outputBlock.Text += output + Environment.NewLine;

      outputBlock.Text += String.Format("Time of Travel: {0:%d} day(s)", 
                                        duration) + Environment.NewLine;
      outputBlock.Text += String.Format("Time of Travel: {0:dd\\.hh\\:mm\\:ss} days", 
                                        duration) + Environment.NewLine;
   }
}
// The example displays the following output:
//       Time of Travel: 1 days
//       Time of Travel: 01.12:24:02
//       Time of Travel: 1 day(s)
//       Time of Travel: 01.12:24:02 days

字符串转TimeSpan

using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string value = null;
      TimeSpan interval;

      value = "6";
      if (TimeSpan.TryParseExact(value, "%d", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;

      value = "16:32.05";
      if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;

      value= "12.035";
      if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
         outputBlock.Text += String.Format("{0} --> {1}", value, 
                                           interval.ToString("c")) + Environment.NewLine;
      else
         outputBlock.Text += String.Format("Unable to parse '{0}'", value) + Environment.NewLine;
   }
}
// The example displays the following output:
//       6 --> 6.00:00:00
//       16:32.05 --> 00:16:32.0500000
//       12.035 --> 00:00:12.0350000

参数表格

The following table describes the custom date and time format specifiers.