当前位置:首页 > 开发 > C# > 正文内容

C# 时间操作

C#3年前 (2022-10-01)
获取系统已运行时间    
System.Environment.TickCount;


TimeSpan    
自定义日时分秒    
TimeSpan timeSpan = new TimeSpan(01,12, 34, 56);
Console.WriteLine(timeSpan.ToString());

输出

1.23:45:56


自定义时分秒    
TimeSpan timeSpan = new TimeSpan(12,34,56);
Console.WriteLine(timeSpan.ToString());

输出

12:34:56




DateTime    

 

DateTime dateTime = DateTime.Now;

Console.WriteLine("时间:\t{0}\n",dateTime.ToString());
Console.WriteLine("年\t{0}",dateTime.Year);
Console.WriteLine("月\t{0}",dateTime.Month);
Console.WriteLine("日\t{0}",dateTime.Day);
Console.WriteLine("时\t{0}",dateTime.Hour);
Console.WriteLine("分\t{0}",dateTime.Minute);
Console.WriteLine("秒\t{0}",dateTime.Second);
Console.WriteLine("\n");
Console.WriteLine("年月日\t{0}",dateTime.Date);
Console.WriteLine("时分秒\t{0}",dateTime.TimeOfDay);
Console.WriteLine("毫秒\t{0}",dateTime.Millisecond);
Console.WriteLine("星期\t{0}",((int)dateTime.DayOfWeek));
Console.WriteLine("今年第\t{0}天",dateTime.DayOfYear);

输出

时间:   2022/10/6 19:44:03

年      2022
月      10
日      6
时      19
分      44
秒      3


年月日  2022/10/6 0:00:00
时分秒  19:44:03.4087787
毫秒    408
星期    4
今年第  279天



转载请注明出处。

本文链接:http://www.pythonopen.com/?id=217

返回列表

上一篇:C# 正则表达式

下一篇:C# NPOI

相关文章

C#解析Torrent获取磁力链

NuGet添加 MonoTorrentusing MonoTorrent;string&n...

在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式

要在 C# 中实现类似于 Windows 资源管理器的“名称”排序方式,你需要考虑以下几点:1. 不...

C# 缩减代码量的一些方式

static void Main() { Thread thre...

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# Winform 拖放文件

private void Form1_Load(object send...

C# BackgroundWorker,在DoWork里更新控件内容

一般情况下不可以直接在BackgroundWorker的DoWork事件中更新 UI 控件在Back...