迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 C# 中格式化日期时间

作者:迹忆客 最近更新:2024/01/16 浏览次数:

本教程将演示如何使用 ToString()String.Format 函数从 DateTime 变量创建格式化字符串。

日期格式说明符是 DateTime 变量中不同组件的字符串表示形式。你可以在 ToString()String.Format 函数中使用它们来指定你自己的自定义 DateTime 格式。

以下是一些常用的日期格式说明符。

样品日期:08/09/2021 下午 3:25:10

自定义日期时间格式说明符:

格式说明符 输出 日期时间部分
d 8
dd 08
M 9
MM 09
MMM Sep
MMMM September
yy 21
yyyy 2021
hh 03 小时
HH 15 小时
mm 25 分钟
ss 10
tt PM 时间

标准日期时间格式说明符:

说明符 DateTimeFormatInfo 属性 模式值
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
M MonthDayPattern MMMM dd

有关 DateTime 格式说明符的更详细列表和更多示例,你可以查看以下 Microsoft 的官方文档:

  • 标准
  • 自定义

在 C# 中使用 ToString() 格式化日期时间

DateTime 格式化为字符串就像将 ToString() 应用于 DateTime 并提供你想要使用的格式一样简单。

DateTime.Now.ToString("ddMMyyyy") DateTime.Now.ToString("d")

例子:

using System;

namespace ConsoleApp1 {
  class Program {
    static void Main(string[] args) {
      // Initialize the datetime value
      DateTime date = new DateTime(2021, 12, 18, 16, 5, 7, 123);

      // Format the datetime value to a standard format (ShortDatePattern)
      string stanadard_fmt_date = date.ToString("d");

      // Format the datetime value to a custom format
      string custom_fmt_date = date.ToString("ddMMyyyy");

      // Print the datetime formatted in the different ways
      Console.WriteLine("Plain DateTime: " + date.ToString());
      Console.WriteLine("Standard DateTime Format: " + stanadard_fmt_date);
      Console.WriteLine("Custom DateTime Format: " + custom_fmt_date);
    }
  }
}

输出:

Plain DateTime: 18/12/2021 4:05:07 pm
Standard DateTime Format: 18/12/2021
Custom DateTime Format: 18122021

在 C# 中使用 String.Format() 格式化 DateTime

使用 String.Format 方法将 DateTime 格式化为字符串是通过在参数中提供格式和 DateTime 值来完成的。打印格式时,格式必须用括号括起来并以"0:"为前缀。

String.Format("{0:ddMMyyyy}", DateTime.Now) String.Format("{0:d}", DateTime.Now)

例子:

using System;

namespace ConsoleApp1 {
  class Program {
    static void Main(string[] args) {
      // Initialize the datetime value
      DateTime date = new DateTime(2021, 12, 18, 16, 5, 7, 123);

      // Format the datetime value to a standard format (ShortDatePattern)
      string stanadard_fmt_date = String.Format("{0:d}", date);

      // Format the datetime value to a custom format
      string custom_fmt_date = String.Format("{0:ddMMyyyy}", date);

      // Print the datetime formatted in the different ways
      Console.WriteLine("Plain DateTime: " + date.ToString());
      Console.WriteLine("Standard DateTime Format: " + stanadard_fmt_date);
      Console.WriteLine("Custom DateTime Format: " + custom_fmt_date);
    }
  }
}

输出:

Plain DateTime: 18/12/2021 4:05:07 pm
Standard DateTime Format: 18/12/2021
Custom DateTime Format: 18122021

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便