C 標準庫獲取當前系統時間
儘量用C標準庫、不用系統API
首先包含標頭檔案
#include <time.h>
返回一個string吧
string getCurrentTime()
{
time_t t=time(0);
char tmp[64];
strftime(tmp,sizeof(tmp),"%Y/%m/%d %X %A",localtime((&t)));
string timeStr=tmp;
return timeStr;
};
strftime() 函式
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
我們可以根據 format 指向字串中格式命令把timeptr中儲存的時間資訊放在strDest指向的字串中,最多向strDest中存放maxsize個字元。該函式返回向strDest指向的字串中放置的字元數。
%a 星期幾的簡寫 %A 星期幾的全稱 %b 月份的簡寫 %B 月份的全稱 %c 標準的日期的時間串 %C 年份的後兩位數字 %d 十進位制表示的每月的第幾天 %D 月/天/年 %e 在兩字元域中,十進位制表示的每月的第幾天 %F 年-月-日 %g 年份的後兩位數字,使用基於周的年 %G 年份,使用基於周的年 %h 簡寫的月份名 %H 24小時制的小時 %I 12小時制的小時 %j 十進位制表示的每年的第幾天 %m 十進位制表示的月份 %M 十時製表示的分鐘數 %n 新行符 %p 本地的AM或PM的等價顯示 %r 12小時的時間 %R 顯示小時和分鐘:hh:mm %S 十進位制的秒數 %t 水平製表符 %T 顯示時分秒:hh:mm:ss %u 每週的第幾天,星期一為第一天 (值從1到7,星期一為1) %U 第年的第幾周,把星期日作為第一天(值從0到53) %V 每年的第幾周,使用基於周的年 %w 十進位制表示的星期幾(值從0到6,星期天為0) %W 每年的第幾周,把星期一做為第一天(值從0到53) %x 標準的日期串 %X 標準的時間串 %y 不帶世紀的十進位制年份(值從0到99) %Y 帶世紀部分的十制年份 %z,%Z 時區名稱,如果不能得到時區名稱則返回空字元。 %% 百分號
如果想顯示現在是幾點了,並以12小時制顯示,就象下面這段程式:
#include "time.h"
#include "stdio.h"
int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(<);
strftime(str,sizeof(str),"It is now %I %p",ptr);
printf("%s\n",str);
return 0;
}
其執行結果為: It is now 4PM 而下面的程式則顯示當前的完整日期:
#include<stdio.h> #include<time.h> int main( void ) { struct tm *newtime; char tmpbuf[128]; time_t lt1; time(<1); newtime=localtime(<1); strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y.\n", newtime); printf("%s\n",tmpbuf); return 0; }
相關推薦
C 標準庫獲取當前系統時間
儘量用C標準庫、不用系統API 首先包含標頭檔案 #include <time.h> 返回一個string吧 string getCurrentTime() { time_t t=ti
c#獲取當前系統時間,並提取按格式提取年月日為字符串
sta ogr mon 系統 ram AR 當前系統時間 ren 獲取 class Program { static void Main(String[] args) { DateTime currentTi
C C++如何獲取當前系統時間
C庫中與系統時間相關的函式定義在<time.h>標頭檔案中, C++定義在<ctime>標頭檔案中。 一、time(time_t*)函式 函式定義如下: time_t time (time_t* timer);
c++獲取當前系統時間並格式化輸出
#include <string> #include <time.h> using namespace std; string getTime() { time_t timep; time (&timep); char
Linux下用C獲取當前系統時間
#include <time.h> time_t time(time_t calptr); 返回的是日曆時間,即國際標準時間公元1970年1月1日00 : 00 : 00以來經過的秒數。然後再呼叫 char *ctime(const
C++/C 獲取當前系統時間
//方案— 優點:僅使用C標準庫;缺點:只能精確到秒級 #include <time.h> #include <stdio.h> int main( void ) { time_t t = time(0); char tmp[64]; strftim
C/C++ 獲取當前系統時間到字串
#include <time.h> #include <stdlib.h> char *pszCurrTime = (char*)malloc(sizeof(char)*20); memset(pszCurrTime, 0, sizeof(cha
C中獲取當前系統時間
#include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo
C++標準庫獲取時間、簡單的檔案操作
參考文章: 只是在以上的一堆,借鑑了一個簡單的用C++標準庫的日期及其格式化程式。 程式如下:(QT下面寫的) #include <chrono> #include <ctime> #include <string> #in
C/C++獲取當前系統時間
//方案— 優點:僅使用C標準庫;缺點:只能精確到秒級 #include <time.h> #include <stdio.h> int main( void ) { time_t t = time(0); char tmp[64]; strftim
JAVA中獲取當前系統時間
arch tac 獲取 -m simple 得到 轉化 sel stack 一. 獲取當前系統時間和日期並格式化輸出: import java.util.Date;import java.text.SimpleDateFormat; public class NowStri
JAVA獲取當前系統時間System.currentTimeMillis()
log time 格式 formatter simple 但是 計算 之間 自己 System.currentTimeMillis()產生一個當前的毫秒,這個毫秒其實就是自1970年1月1日0時起的毫秒數,Date()其實就是相當於Date(System.currentTi
java---獲取當前系統時間--納秒
系統時間 star pretty 開始 min gpo post java pan long startTime = System.nanoTime(); //開始時間 //這裏寫操作 //消耗時間 long consumingTime = System.na
獲取當前系統時間工具類TimeUtils
string public date() new edate 當前 com strong alt TimeUtils: package com.duocy.util; import java.time.LocalDateTime;import java.time.f
獲取當前系統時間的前一天,後一天
獲取當前系統前一天日期 public static Date getYesterday(Date date) { Calendar calendar = Calendar.getInstance
Jmeter入門12 __time函式 jmeter獲取當前系統時間
有的介面要傳遞當前的日期或時間,可以用__time()函式獲取當前時間 ${__time()} 當前時間到計算機元年的毫秒數 ${__time(時間格式)} 以預定的格式顯示當前時間 請求示例: __time(時間格式)&nbs
js獲取當前系統時間的前幾小時、天、周、月、年
**.js $(function(){ }); var oneJsTest={ //以當前系統時間作為結束時間,return不同時間範圍de開始時間 reqShebeiDataByShebeiAndTimeQueryByDifferentTime:func
jq獲取當前系統時間
//格式:2018-10-08 16:21:25 var d = new Date(); var vYear = d.getFullYear() var vMon = d.getMonth() + 1 var vDay = d.getDate(); var h = d.ge
jquery獲取當前系統時間/當前系統第二天時間/當前系統第三天時間
/**載入日期*/ function quickInputCustDateTimePicker(){ /** 獲取當前系統時間 */ &
獲取當前系統時間兩小時後和四小時後,蘋果瀏覽器報錯NaN,或者Invalid Date,
<script type="text/javascript" src="js/libs/jquery.min.js"></script> <script type="text/javascript" src="j