格式化一个时间字符串
2007-12-17 10:50
260 查看
格式化一个时间字符串
Article last modified on 2002-4-30
----------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual C++ 6.0
----------------------------------------------------------------
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=12664
Article last modified on 2002-4-30
----------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual C++ 6.0
----------------------------------------------------------------
利用Run-Time Library
具体的用法范例[/b]:[/b] string strTime; CurrentTimeStr(&strTime, TRUE); CurrentTimeStr定义为: [/b][/b]#include <Time.h> ////////////////////////////////////////////////////////////////////// // // STL的常用头文件和声明: // ////////////////////////////////////////////////////////////////////// #pragma warning(disable:4786) #include <string> using namespace std; // ////////////////////////////////////////////////////////////////////// // // 构造出当前时间的字符串 // void CurrentTimeStr(string *pstrTimeString, BOOL bWantMore) { char szTimeString[128]; memset(szTimeString, 0, 128); 使用的结构[/i]tm[/i]的说明[/i]:[/i] 在[/i]WCHAR.H[/i]中定义[/i]:[/i] #ifndef _TM_DEFINED[/i] struct tm {[/i] int tm_sec; /* seconds after the minute - [0,59] */[/i] int tm_min; /* minutes after the hour - [0,59] */[/i] int tm_hour; /* hours since [/i]midnight[/i] - [0,23] */[/i] int tm_mday; /* day of the month - [1,31] */[/i] int tm_mon; /* months since January - [0,11] */[/i] int tm_year; /* years since 1900 */[/i] int tm_wday; /* days since Sunday - [0,6] */[/i] int tm_yday; /* days since January 1 - [0,365] */[/i] int tm_isdst; /* daylight savings time flag */[/i] };[/i] #define _TM_DEFINED[/i] #endif[/i] struct tm *today; time_t[/i]类型是在[/i]WCHAR.H[/i]中定义[/i]:[/i] #ifndef _TIME_T_DEFINED[/i] typedef long time_t;[/i] #define _TIME_T_DEFINED[/i] #endif[/i][/i] time_t ltime; 定义在[/i]Time.H[/i]中[/i][/i] Set time zone from TZ environment variable. If TZ is not set,[/i] the operating system is queried to obtain the default value [/i] for the variable. [/i][/i] _tzset(); 定义在[/i]Time.H[/i]中[/i][/i] Get UNIX-style time and display as number and string.[/i] [/i] time( <ime ); 这个函数的定义是:[/i][/i] struct tm * __cdecl localtime(const time_t *);[/i] 它主要是用来[/i]Use time structure to build a customized time string[/i] and correct for the local time zone[/i]. [/i] 它将返回一个指针,指向[/i]tm[/i]的结构,如果这个时间值是早于[/i]1970[/i]年[/i]1[/i]月[/i]1[/i]日午夜的,则会返回[/i]NULL[/i]。[/i][/i] today = localtime( <ime ); strftime[/i]指定的第一个参数就是目标字符串,第二个参数就是该目标字符串的最大[/i][/i] 长度;[/i][/i] 第三个参数将制定格式化的定义;[/i][/i] 第四个参数是时间结构[/i]tm[/i] 下面说明格式化命令的含义:[/i][/i] %m[/i] [/i] Month as decimal number (01 – 12) [/i] %d[/i] [/i] Day of month as decimal number (01 – 31)[/i] %H[/i] [/i] Hour in 24-hour format (00 – 23) [/i] %M[/i] [/i] Minute as decimal number (00 – 59) [/i] %S[/i] [/i] Second as decimal number (00 – 59) [/i] strftime( szTimeString, 128, "=时间: %m月%d日%H时%M分%S秒 ", today ); (*pstrTimeString).append(szTimeString); } // |
相关文章推荐
- 获取系统当前时间,完成格式化,得到一个字符串。例如”201608130743”(指年月日时分)。再随机取3个小于10的整数,分别插入到 字符串的年、月、日后面,完成输出。
- 如何将一个格林威治字符串时间格式化转换为本地时间
- 格式化一个时间字符串
- 格式化一个时间字符串
- 系统日期时间格式化与字符串替换函数
- C#格式化字符串,日期,时间,货币[转]
- C# format 格式化 日期时间 数字 字符串 datetime
- JSP页面使用JSTL的function标签库截取字符串及格式化时间
- 在一个时间字符串数组中找出离当前时间字符串最近的前后两个时间
- 随机产生一个跟时间相关的字符串
- 格式化数字字符串输出的一个小技巧
- 格式化字符串android 格式化时间
- 通过format方法格式化字符串和日期时间
- Flex中一个关于时间格式化的函数
- 给定一个字符串,查找该字符串中第一个重复出现的字符索引。要求:时间复杂度为O(n)。
- java_日期和时间字符串格式化
- Java获取当前时间年月日、时间格式化打印、字符串转日期
- js 将时间戳格式化为 yyyy-MM-dd HH:mm:ss 格式的字符串
- 一个关于数字和字符串格式化的示例
- iOS 获取当前时间格式化字符串