如:Mon Sep 22 15:58:27 2003 转换为 20030922 呢?或者说有其它的方法也行,谢谢大家。
附一函数,这个时间就是这样提取的,谢谢大家。ANSI标准的函数localtime(),描述如下:
Synopsis:
#include <time.h>
struct tm * localtime( const time_t *timer );
struct tm {
int tm_sec; /* seconds after the minute -- [0,61] */
int tm_min; /* minutes after the hour -- [0,59] */
int tm_hour; /* hours after midnight -- [0,23] */
int tm_mday; /* day of the month -- [1,31] */
int tm_mon; /* months since January -- [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday -- [0,6] */
int tm_yday; /* days since January 1 -- [0,365]*/
int tm_isdst; /* Daylight Savings Time flag */
};
Description:
The localtime functions convert the calendar time pointed to by timer into a structure of type tm, of time information, expressed as local time. Whenever localtime is called, the tzset function is also called.
The calendar time is usually obtained by using the time function. That time is Coordinated Universal Time (UTC) (formerly known as Greenwich Mean Time (GMT)).
The time set on the computer with the DOS time command and the DOS date command reflects the local time. The environment variable TZ is used to establish the time zone to which this local time applies. See the section The TZ Environment Variable for a discussion of how to set the time zone.
Returns:
The localtime functions return a pointer to a tm structure containing the time information.
See Also:
asctime, clock, ctime, difftime, gmtime, mktime, strftime, time, tzset
Example:
#include <stdio.h>
#include <time.h>
void main()
{
time_t time_of_day;
auto char buf[26];
auto struct tm tmbuf;
time_of_day = time( NULL );
_localtime( &time_of_day, &tmbuf );
printf( "It is now: %s", _asctime( &tmbuf, buf ) );
}
produces the following:
It is now: Sat Mar 21 15:58:27 1987
Classification:
localtime is ANSI
char szTime[14];
SYSTEMTIME m_systime;
GetLocalTime(&m_systime);
sprintf(szTime,"%04d%02d%02d%02d%02d%02d",m_systime.wYear,m_systime.wMonth,m_systime.wDay,m_systime.wHour,m_systime.wMinute,m_systime.wSecond);
/* TIMES.C illustrates various time and date functions including:
* time _ftime ctime asctime
* localtime gmtime mktime _tzset
* _strtime _strdate strftime
*
* Also the global variable:
* _tzname
*/
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>
void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();
/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );
/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );
/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;
/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );
/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );
/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
/* Use time structure to build a customized time string. */
today = localtime( <ime );
/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}
C语言风格的,几乎有所有的时间格式,
功 能: 取DOS日期
用 法: void getdate(struct *dateblk);
程序例:
#include <dos.h>
#include <stdio.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n",
d.da_year);
printf("The current day is: %d\n",
d.da_day);
printf("The current month is: %d\n",
d.da_mon);
return 0;
}
sprintf()+格式输入