TimeSpan格式化字元串格式(摘)

来源:https://www.cnblogs.com/adinet/archive/2018/04/15/8841442.html
-Advertisement-
Play Games

一直在用DateTime, 卻不常用TimeSpan , 今天突然用到了, 發現不知道咋做格式化...百度一下,找到了答案, 在這記錄一下, 免得以後找花費時間 以下內容摘抄自 Microsoft Docs 原文地址: https://docs.microsoft.com/en-us/previou ...


一直在用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.

 

Format specifier

Description

Example

"d", "%d"

The number of whole days in the time interval.

More information: The "d" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   %d --> "6"

   d\.hh\:mm --> "6.14:32"

"dd"-"dddddddd"

The number of whole days in the time interval, padded with leading zeros as needed.

More information: The "dd"-"dddddddd" Custom Format Specifiers.

new TimeSpan(6, 14, 32, 17, 685):

   ddd --> "006"

   dd\.hh\:mm --> "06.14:32"

"h", "%h"

The number of whole hours in the time interval that are not counted as part of days. Single-digit hours do not have a leading zero.

More information: The "h" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   %h --> "14"

   hh\:mm --> "14:32"

"hh"

The number of whole hours in the time interval that are not counted as part of days. Single-digit hours have a leading zero.

More information: The "hh" Custom Format Specifier.

new TimeSpan(6, 14, 32, 17, 685):

   hh --> "14"

new TimeSpan(6, 8, 32, 17, 685):

   hh --> 08

"m", "%m"

The number of whole minutes in the time interval that are not included as part of hours or days. Single-digit minutes do not have a leading zero.

More information: The "m" Custom Format Specifier.

new TimeSpan(6, 14, 8, 17, 685):

   %m --> "8"

   h\:m --> "14:8"

"mm"

The number of whole minutes in the time interval that are not included as part of hours or days. Single-digit minutes have a leading zero.

More information: The "mm" Custom Format Specifier.

new TimeSpan(6, 14, 8, 17, 685):

   mm --> "08"

new TimeSpan(6, 8, 5, 17, 685):

   d\.hh\:mm\:ss --> 6.08:05:17

"s", "%s"

The number of whole seconds in the time interval that are not included as part of hours, days, or minutes. Single-digit seconds do not have a leading zero.

More information: The "s" Custom Format Specifier.

TimeSpan.FromSeconds(12.965):

   %s --> 12

   s\.fff --> 12.965

"ss"

The number of whole seconds in the time interval that are not included as part of hours, days, or minutes. Single-digit seconds have a leading zero.

More information: The "ss" Custom Format Specifier.

TimeSpan.FromSeconds(6.965):

   ss --> 06

   ss\.fff --> 06.965

"f", "%f"

The tenths of a second in a time interval.

More information: The "f" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   f --> 8

   ss\.f --> 06.8

"ff"

The hundredths of a second in a time interval.

More information: The "ff" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   ff --> 89

   ss\.ff --> 06.89

"fff"

The milliseconds in a time interval.

More information: The "fff" Custom Format Specifier.

TimeSpan.FromSeconds(6.895):

   fff --> 895

   ss\.fff --> 06.895

"ffff"

The ten-thousandths of a second in a time interval.

More information: The "ffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   ffff --> 8954

   ss\.ffff --> 06.8954

"fffff"

The hundred-thousandths of a second in a time interval.

More information: The "fffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   fffff --> 89543

   ss\.fffff --> 06.89543

"ffffff"

The millionths of a second in a time interval.

More information: The "ffffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   ffffff --> 895432

   ss\.ffffff --> 06.895432

"fffffff"

The ten-millionths of a second (or the fractional ticks) in a time interval.

More information: The "fffffff" Custom Format Specifier.

TimeSpan.Parse("0:0:6.8954321"):

   fffffff --> 8954321

   ss\.fffffff --> 06.8954321

"F", "%F"

The tenths of a second in a time interval. Nothing is displayed if the digit is zero.

More information: The "F" Custom Format Specifier.

TimeSpan.Parse("00:00:06.32"):

   %F: 3

TimeSpan.Parse("0:0:3.091"):

   ss\.F: 03.

"FF"

The hundredths of a second in a time interval. Any fractional trailing zeros or two zero digits are not included.

More information: The "FF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.329"):

   FF: 32

TimeSpan.Parse("0:0:3.101"):

   ss\.FF: 03.1

"FFF"

The milliseconds in a time interval. Any fractional trailing zeros are not included.

More information:

TimeSpan.Parse("00:00:06.3291"):

   FFF: 329

TimeSpan.Parse("0:0:3.1009"):

   ss\.FFF: 03.1

"FFFF"

The ten-thousandths of a second in a time interval. Any fractional trailing zeros are not included.

More information: The "FFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.32917"):

   FFFFF: 3291

TimeSpan.Parse("0:0:3.10009"):

   ss\.FFFF: 03.1

"FFFFF"

The hundred-thousandths of a second in a time interval. Any fractional trailing zeros are not included.

More information: The "FFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.329179"):

   FFFFF: 32917

TimeSpan.Parse("0:0:3.100009"):

   ss\.FFFFF: 03.1

"FFFFFF"

The millionths of a second in a time interval. Any fractional trailing zeros are not displayed.

More information: The "FFFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 329179

TimeSpan.Parse("0:0:3.1000009"):

   ss\.FFFFFF: 03.1

"FFFFFFF"

The ten-millions of a second in a time interval. Any fractional trailing zeros or seven zeros are not displayed.

More information: The "FFFFFFF" Custom Format Specifier.

TimeSpan.Parse("00:00:06.3291791"):

   FFFFFF: 3291791

TimeSpan.Parse("0:0:3.1900000"):

   ss\.FFFFFF: 03.19

'string'

Literal string delimiter.

More information: Other Characters.

new TimeSpan(14, 32, 17):

   hh':'mm':'ss --> "14:32:17"

\

The escape character.

More information: Other Characters.

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

Any other character

Any other unescaped character is interpreted as a custom format specifier.

More Information: Other Characters.

new TimeSpan(14, 32, 17):

   hh\:mm\:ss --> "14:32:17"

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 完成九宮格程式 在井字型的格局中(奇數格局),放入數字,使得每行每列以及斜對角線的和都相等 經驗規則:從1開始按順序逐個填寫,1放在第一行的中間位置,下一個數往右上角45度 處填寫。如果單邊越界則按頭尾相接地填;如果有衝突,則填在剛纔位置的底下一格 如果雙邊越界,則填在剛纔位置的底下一格。 ...
  • You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the v ...
  • c#和Java: 1.首先,子類繼承了父類的屬性和方法,但是子類並沒有繼承父類的私有欄位。 2.子類並沒有繼承父類的構造函數,但是。子類會預設的調用父類無參數的構造函數,創建父類對象,讓子類可以使用父類中的成員。 所以,如果在父類中重新寫了一個有參數的構造函數之後,那個無參數的就被幹掉了,子類就調用 ...
  • Spring AOP 簡介 如果說 IoC 是 Spring 的核心,那麼面向切麵編程就是 Spring 最為重要的功能之一了,在資料庫事務中切麵編程被廣泛使用。 AOP 即 Aspect Oriented Program 面向切麵編程 首先,在面向切麵編程的思想裡面,把功能分為核心業務功能,和周邊 ...
  • Collections :它的出現給集合操作提供了更多的功能。這個類不需要創建對象,內部提供的都是靜態方法。 Arrays :用於操作數組對象的工具類,方法皆為靜態方法。 ...
  • PHP的日期和時間庫 驗證日期 checkbox()函數 格式化日期和時間 Y年 m月 d日 H時 i分 s秒 date()函數 將時間戳轉換用戶友好的值 getdate()函數 處理時間戳 確定當前的時間戳 time()函數 echo time();//獲取當前的時間戳1523712349 將時間 ...
  • 面向對象--介面 ...
  • 一。添加控制項lrisSkin.dll 然後把繼承的窗體更換成別人做好的窗體類 能達到換膚的效果 二。 全部源代碼就一行: skinEngine1.SkinFile = "WaveColor1.ssk"; 其中ssk文件為皮膚文件。 如果上面的不行,就設置skinEngine1控制項的SkinFile屬 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...