C 獲取系統時間及時間格式

2021-08-10 02:29:04 字數 4029 閱讀 9743

--datetime 數字型 

system.datetime currenttime=new system.datetime(); 

取當前年月日時分秒      currenttime=system.datetime.now; 

取當前年     int 年=currenttime.year; 

取當前月     int 月=currenttime.month; 

取當前日     int 日=currenttime.day; 

取當前時     int 時=currenttime.hour; 

取當前分     int 分=currenttime.minute; 

取當前秒     int 秒=currenttime.second; 

取當前毫秒    int 毫秒=currenttime.millisecond; (變數可用中文)

取中文日期顯示——年月日時分    string stry=currenttime.tostring("f"); //不顯示秒

取中文日期顯示_年月       string strym=currenttime.tostring("y");

取中文日期顯示_月日     string strmd=currenttime.tostring("m");

取當前年月日,格式為:2003-9-23      string strymd=currenttime.tostring("d");

取當前時分,格式為:14:24      string strt=currenttime.tostring("t");

datetime.now.tostring();//獲取當前系統時間 完整的日期和時間

datetime.now.tolongdatestring();//只顯示日期 ***x年xx月xx日 ,乙個是長日期

datetime.now.toshortdatestring();//只顯示日期 ***x-xx-xx 乙個是短日期

//今天        datetime.now.date.toshortdatestring();

//昨天 的       datetime.now.adddays(-1).toshortdatestring();

//明天 的       datetime.now.adddays(1).toshortdatestring();

//本週(注意這裡的每一周是從週日始至週六止)

datetime.now.adddays(convert.todouble((0 - convert.toint16(datetime.now.dayofweek)))).toshortdatestring();

datetime.now.adddays(convert.todouble((6 - convert.toint16(datetime.now.dayofweek)))).toshortdatestring();

//上週,上週就是本週再減去7天

datetime.now.adddays(convert.todouble((0 - convert.toint16(datetime.now.dayofweek))) - 7).toshortdatestring();

datetime.now.adddays(convert.todouble((6 - convert.toint16(datetime.now.dayofweek))) - 7).toshortdatestring();

//下週    本週再加上7天

datetime.now.adddays(convert.todouble((0 - convert.toint16(datetime.now.dayofweek))) + 7).toshortdatestring();

datetime.now.adddays(convert.todouble((6 - convert.toint16(datetime.now.dayofweek))) + 7).toshortdatestring();

//本月    本月的第一天是1號,最後一天就是下個月一號再減一天。

datetime.now.year.tostring() + datetime.now.month.tostring() + "1"; //第一天

datetime.parse(datetime.now.year.tostring() + datetime.now.month.tostring() + "1").addmonths(1).adddays(-1).toshortdatestring();//最後一天

另一種方法:

datetime now = datetime.now; 

datetime d1 = new datetime(now.year, now.month, 1); //本月第一天

datetime d2 = d1.addmonths(1).adddays(-1); //本月最後一天

ps:datetime.now.dayofweek.tostring();//英文星期顯示,wednesday

(int)datetime.now.dayofweek     數字,若是週三,結果對應為3

datetime.now.tostring("dddd", new system.globalization.cultureinfo("zh-cn")); //中文星期顯示

datetime.now.tostring("dddd");//中文星期顯示

datetime.now.tostring("dddd,mmmm,dd ,yyyy", new system.globalization.datetimeformatinfo());//顯示日期格式friday,july, 01,2009

datetime.now.tostring("dddd,dd mmmm,yyyy") //輸出   星期三,30 一月,2008

出處:如何:從特定日期中提取星期幾

datetime型別在tostring()format的格式設定

引數format格式詳細用法 

格式字元 關聯屬性/說明 

d shortdatepattern 

d longdatepattern 

f 完整日期和時間(長日期和短時間) 

f fulldatetimepattern(長日期和長時間) 

g 常規(短日期和短時間) 

g 常規(短日期和長時間) 

m、m monthdaypattern 

r、r rfc1123pattern 

s 使用當地時間的 sortabledatetimepattern(基於 iso 8601) 

t shorttimepattern 

t longtimepattern 

u universalsortabledatetimepattern 用於顯示通用時間的格式 

u 使用通用時間的完整日期和時間(長日期和長時間) 

y、y yearmonthpattern 

下表列出了可被合併以構造自定義模式的模式。這些模式是區分大小寫的

d 月中的某一天。一位數的日期沒有前導零。 

dd 月中的某一天。一位數的日期有乙個前導零。 

ddd 週中某天的縮寫名稱,在 abbreviateddaynames 中定義。 

dddd 週中某天的完整名稱,在 daynames 中定義。 

m 月份數字。一位數的月份沒有前導零。 

mm 月份數字。一位數的月份有乙個前導零。 

mmm 月份的縮寫名稱,在 abbreviatedmonthnames 中定義。 

mmmm 月份的完整名稱,在 monthnames 中定義。 

y 不包含紀元的年份。如果不包含紀元的年份小於 10,則顯示不具有前導零的年份。 

yy 不包含紀元的年份。如果不包含紀元的年份小於 10,則顯示具有前導零的年份。 

yyyy 包括紀元的四位數的年份。 

gg 時期或紀元。如果要設定格式的日期不具有關聯的時期或紀元字串,則忽略該模式。

h 12 小時制的小時。一位數的小時數沒有前導零。 

hh 12 小時制的小時。一位數的小時數有前導零。 

h 24 小時制的小時。一位數的小時數沒有前導零。 

hh 24 小時制的小時。一位數的小時數有前導零。 

C 獲取系統時間及時間格式

datetime 數字型 system.datetime currenttime new system.datetime 取當前年月日時分秒 currenttime system.datetime.now 取當前年 int 年 currenttime.year 取當前月 int 月 currentt...

C 獲取系統時間及時間格式

datetime 數字型 system.datetime currenttime new system.datetime 取當前年月日時分秒 currenttime system.datetime.now 取當前年 int 年 currenttime.year 取當前月 int 月 currentt...

python獲取時間及時間格式轉換

整理總結一下python中最常用的一些時間戳和時間格式的轉換 第一部分 獲取當前時間和10位13位時間戳 import datetime,time 獲取當前時間 n datetime.datetime.now print n 獲取10位時間戳 now time.time print int now ...