歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言中獲得系統當前日期 和 時間

C語言中獲得系統當前日期 和 時間

日期:2017/3/1 10:17:09   编辑:Linux編程

C語言中獲得系統當前的日期和時間

1.

  1. #include <stdio.h>
  2. #include <time.h>
  3. int main()
  4. {
  5. time_t timep;
  6. struct tm *p;
  7. time(&timep);
  8. p =localltime(&timep); //此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
  9. //p = gmtime(&timep); //把日期和時間轉換為格林威治(GMT)時間的函數
  10. printf("Year: %d\n", 1900+p->tm_year);
  11. printf("Month: %d\n", 1+p->tm_mon);
  12. printf("Day: %d\n", p->tm_mday);
  13. printf("Hour: %d\n", p->tm_hour);
  14. printf("Minute: %d\n", p->tm_min);
  15. printf("Second: %d\n", p->tm_sec);
  16. printf("Weekday: %d\n", p->tm_wday);
  17. printf("Days: %d\n", p->tm_yday);
  18. printf("Isdst: %d\n", p->tm_isdst);
  19. }

2.

  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: Time04.c
  5. *
  6. * Description: 輸入格式化後的系統當前時間
  7. *
  8. * Version: 1.0
  9. * Created: 2012年07月10日 22時34分31秒
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: ShanHaiyang
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <string.h>
  22. int main()
  23. {
  24. time_t timep;
  25. char s[30];
  26. time(&timep);
  27. strcpy(s,ctime(&timep));
  28. printf("%s\n", s);
  29. }
Copyright © Linux教程網 All Rights Reserved