獲取時間方案

2021-06-01 18:21:17 字數 1938 閱讀 5874

/

方案— 優點:僅使用c標準庫;缺點:只能精確到秒級

#include

<

time.h

>

#include

<

stdio.h

>

intmain(

void)

size_t strftime(

char

*strdest, size_t maxsize,

const

char

*format,

const

struct

tm *

timeptr);

根據格式字串生成字串。

struct

tm *

localtime(

const

time_t

*timer);

取得當地時間,localtime獲取的結果由結構tm返回返回的字串可以依下列的格式而定: %

a 星期幾的縮寫。eg:tue %

a 星期幾的全名。 eg: tuesday %

b 月份名稱的縮寫。 %

b 月份名稱的全名。 %

c 本地端日期時間較佳表示字串。 %

d 用數字表示本月的第幾天 (範圍為

00至

31)。日期 %

h 用

24小時制數字表示小時數 (範圍為

00至

23)。 %

i 用

12小時制數字表示小時數 (範圍為

01至

12)。 %

j 以數字表示當年度的第幾天 (範圍為

001至

366)。 %

m 月份的數字 (範圍由 1至

12)。 %

m 分鐘。 %

p 以

''am

''或

''pm

''表示本地端時間。 %

s 秒數。 %

u 數字表示為本年度的第幾周,第乙個星期由第乙個週日開始。 %

w 數字表示為本年度的第幾周,第乙個星期由第乙個周一開始。 %

w 用數字表示本週的第幾天 (

0為週日)。 %

x 不含時間的日期表示法。 %

x 不含日期的時間表示法。 eg: 15:

26:30

%y 二位數字表示年份 (範圍由

00至

99)。 %

y 完整的年份數字表示,即四位數。 eg:

2008 %

z(%z) 時區或名稱縮寫。eg:中國標準時間 %%

%字元。 //

方案二 優點:能精確到毫秒級;缺點:使用了windows api

#include

<

windows.h

>

#include

<

stdio.h

>

intmain(

void)

//方案三,優點:利用系統函式,還能修改系統時間

//此檔案必須是c++檔案

#include

<

stdlib.h

>

#include

<

iostream

>

using

namespace

std;

void

main()

//方案四,將當前時間折算為秒級,再通過相應的時間換算即可

//此檔案必須是c++檔案

#include

<

iostream

>

#include

<

ctime

>

using

namespace

std;

intmain()

根據時間戳獲取時間整點差8小時解決方案

首先介紹一下 北京時間與格林尼治時間或utc時間相差8個時區,北京 上海 重慶位於東8區,頁面伺服器時間是東八區時間,頁面 js 功能需要對比伺服器時間和使用者本地時間,為相容世界各地時間,需要將使用者本地時間轉換為東八區時間。本地時間 格林威治時間 時差 格林威治時間 本地時間 時差 為時區間的差...

ntp時間獲取

ntp時間獲取 char szip 512 bool bf hostnametoip www.google.com szip if bf printf s n szip systemtime time bool bflag getcurtime time,61.153.197.226 char sz...

php獲取時間

1 獲取當前日期的前一天 thedate date ymd mktime 0,0,0,date m date d 1,date y 2 獲取指定時間的前一天和後一天 yesterday date y m d strtotime thedate.1 day 前一天 tomorrow date y m ...