C#のDateTime種々の従来の操作

我々は、例えば、変換時間が一般的に使用されている開発:現在のシステム時刻を取得し、UTC時間、日付と時刻フォーマットを取得し、時間帯0に変換され、現在のシステム帯域、動作の時間を、単一の日付と時間がかかります。

まず、アクセス日時


//获取当前系统时间
var currentTime = DateTime.Now;
//获取UTC时间
var utc = DateTime.UtcNow;

第二に、日付と時刻の形式

//获取当前系统时间
var currentTime = DateTime.Now;
Console.WriteLine(currentTime.ToString("yyyy/MM/dd HH:mm:ss"));
Console.WriteLine(currentTime.ToString("yyyy/MM/dd"));
Console.WriteLine(currentTime.ToString("HH:mm:ss"));

第三に、タイムゾーンの変換日時

//获取UTC时间
var utc1 = DateTime.UtcNow;
//将UTC时间转换成当前系统时区
var currentDateTime = timeZone.ToLocalTime(utc1);
//将当前系统时间转换成UTC时间
var utc2 = timeZone.ToUniversalTime(currentDateTime);
Console.WriteLine("UTC日期时间:{0}",utc1);
Console.WriteLine("将UTC时间转换成当前系统时区的日期时间:{0}",currentDateTime);
Console.WriteLine("将当前系统时间转换成UTC时间的日期时间:{0}", utc2);

第四に、シングルを取るために、日付と時刻

//获取当前系统时间
var currentDateTime = DateTime.Now;
//年
Console.WriteLine("年:" + currentDateTime.Year);
//月
Console.WriteLine("月:" + currentDateTime.Month);
//日
Console.WriteLine("日:" + currentDateTime.Day);
//小时
Console.WriteLine("小时:" + currentDateTime.Hour);
//分钟
Console.WriteLine("分钟:" + currentDateTime.Minute);
//秒
Console.WriteLine("秒:" + currentDateTime.Second);
//毫秒
Console.WriteLine("毫秒:" + currentDateTime.Millisecond);

第五に、操作の日付と時刻

//获取当前系统时间
var currentDateTime = DateTime.Now;
//加年
Console.WriteLine("加年:" + currentDateTime.AddYears(1));
//加月
Console.WriteLine("加月:" + currentDateTime.AddMonths(1));
//加日
Console.WriteLine("加日:" + currentDateTime.AddDays(1));
//加小时
Console.WriteLine("加小时:" + currentDateTime.AddHours(1));
//加分钟
Console.WriteLine("加分钟:" + currentDateTime.AddMinutes(1));
//加秒
Console.WriteLine("加秒:" + currentDateTime.AddSeconds(1));
//加毫秒
Console.WriteLine("加毫秒:" + currentDateTime.AddMilliseconds(1));

 

公開された21元の記事 ウォンの賞賛0 ビュー601

おすすめ

転載: blog.csdn.net/Stodger0216/article/details/103695509