http://ilewen.com/questions/3990
- int main(int argc, char **argv)
- {
- time_t t;
- t = time(NULL);
- struct tm *lt;
- int ii = time(&t);
- printf("ii = %d\n", ii);
- t = time(NULL);
- lt = localtime(&t);
- char nowtime[24];
- memset(nowtime, 0, sizeof(nowtime));
- strftime(nowtime, 24, "%Y-%m-%d %H:%M:%S", lt);
- printf("nowtime = %s\n", nowtime);
- return 1;
- }
- int main(int argc, char **argv)
- {
- time_t t;
- t = time(NULL);
- struct tm *lt;
- int ii = time(&t);
- printf("ii = %d\n", ii);
- t = time(NULL);
- lt = localtime(&t);
- char nowtime[24];
- memset(nowtime, 0, sizeof(nowtime));
- strftime(nowtime, 24, "%Y-%m-%d %H:%M:%S", lt);
- printf("nowtime = %s\n", nowtime);
- return 1;
- }
打印:
<code><span class="pln">ii </span><span class="pun">=</span><span class="pln"> </span><span class="lit">1325302987</span><span class="pln"> nowtime </span><span class="pun">=</span><span class="pln"> </span><span class="lit">2011</span><span class="pun">-</span><span class="lit">12</span><span class="pun">-</span><span class="lit">31</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">43</span><span class="pun">:</span><span class="lit">07</span></code>
随便输入一个标准格式的时间 “2011-12-31 11:43:07”,转换成时间戳 1325302987
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- long GetTick(char *str_time)
- {
- struct tm stm;
- int iY, iM, iD, iH, iMin, iS;
- memset(&stm,0,sizeof(stm));
- iY = atoi(str_time);
- iM = atoi(str_time+5);
- iD = atoi(str_time+8);
- iH = atoi(str_time+11);
- iMin = atoi(str_time+14);
- iS = atoi(str_time+17);
- stm.tm_year=iY-1900;
- stm.tm_mon=iM-1;
- stm.tm_mday=iD;
- stm.tm_hour=iH;
- stm.tm_min=iMin;
- stm.tm_sec=iS;
- /*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/
- return mktime(&stm);
- }
- int main()
- {
- char str_time[19];
- printf("请输入时间:"); /*(格式:2011-12-31 11:43:07)*/
- gets(str_time);
- printf("%ld\n", GetTick(str_time));
- return 0;
- }
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- long GetTick(char *str_time)
- {
- struct tm stm;
- int iY, iM, iD, iH, iMin, iS;
- memset(&stm,0,sizeof(stm));
- iY = atoi(str_time);
- iM = atoi(str_time+5);
- iD = atoi(str_time+8);
- iH = atoi(str_time+11);
- iMin = atoi(str_time+14);
- iS = atoi(str_time+17);
- stm.tm_year=iY-1900;
- stm.tm_mon=iM-1;
- stm.tm_mday=iD;
- stm.tm_hour=iH;
- stm.tm_min=iMin;
- stm.tm_sec=iS;
- /*printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS);*/
- return mktime(&stm);
- }
- int main()
- {
- char str_time[19];
- printf("请输入时间:"); /*(格式:2011-12-31 11:43:07)*/
- gets(str_time);
- printf("%ld\n", GetTick(str_time));
- return 0;
- }