獲取網路時間

2021-07-03 13:38:42 字數 3178 閱讀 2474

#region

獲取網路時間

//////

獲取中國國家授時中心網路伺服器時間發布的當前時間

//////

public

static

datetime getchinesedatetime()

年\d月\d日";

string

pathr =@"

hrs\s+=\s+\d";

string

patmn =@"

min\s+=\s+\d";

string

patsc =@"

sec\s+=\s+\d";

regex regdt

=new

regex(patdt);

regex reghr

=new

regex(pathr);

regex regmn

=new

regex(patmn);

regex regsc

=new

regex(patsc);

res

=datetime.parse(regdt.match(html).value);

inthr

=getint(reghr.match(html).value,

false

);

intmn

=getint(regmn.match(html).value,

false

);

intsc

=getint(regsc.match(html).value,

false

); res

=res.addhours(hr).addminutes(mn).addseconds(sc);

}catch

return

res;

}//////

從指定的字串中獲取整數

//////

原始的字串

///是否完全匹配,若為false,則返回字串中的第乙個整數數字

///整數數字

private

static

intgetint(

string

origin,

bool

fullmatch)

origin

=origin.trim();

if(!fullmatch)

intres =0

;

int.tryparse(origin,

outres);

return

res;

}#endregion

///

///獲取標準北京時間1

//////

public

static

datetime getstandardtime()

catch

(webexception)

catch

(exception)

finally

return

dt; }

//////

獲取標準北京時間2

//////

public

static

datetime getbeijingtime()

}string

temparray

=html.split(';

');for(

inti =0

; i

<

temparray.length; i++)

string

year

=temparray[

1].substring(temparray[

1].indexof(

"nyear=")

+6);

string

month

=temparray[

2].substring(temparray[

2].indexof(

"nmonth=")

+7);

string

day

=temparray[

3].substring(temparray[

3].indexof(

"nday=")

+5);

string

hour

=temparray[

5].substring(temparray[

5].indexof(

"nhrs=")

+5);

string

minite

=temparray[

6].substring(temparray[

6].indexof(

"nmin=")

+5);

string

second

=temparray[

7].substring(temparray[

7].indexof(

"nsec=")

+5); dt

=datetime.parse(year +"

-"+month +"

-"+day +"

"+hour +"

:"+minite +"

:"+second);

}catch

(webexception)

catch

(exception)

finally

return

dt; }

按 ctrl+c 複製**

按 ctrl+c 複製**

呼叫示例:

datetime dt 

=common.getbeijingtime();

if(dt.tostring() =="

2011-1-1 0:00:00")

8266獲取網路時間

8266獲取網路時間 今天第一次用阿里的部落格寫點東西感受一下.sntp.sync ntp1.aliyun.com function print sync succeeded end,function index print failed index end 用的sntp 然後列印時間 time r...

qt獲取網路時間

獲取網路時間,目的就是獲取最新的時間,而不是電腦的本機時間,這種情況一般應用在程式到時間過期,禁止使用情況下。qt5.7 win8 pro檔案中新增network庫 qstring aaa 19 12 15 qstring bbb qtcpsocket socket new qtcpsocket s...

C 獲取網路時間

具體可參考 public static datetime getutcplus8time 最好加try catch來處理獲取不到的時候的情況處理。這裡只是舉例,剩餘的問題各人根據自己的情況發揮。附送c 有關時間的處理方法,請參考 取得某月的最後一天 方法一 使用算出該月多少天,年 月 加上多少天即得...