大家好,请问怎样获得格式化的系统当前日期,就是返回值是这个样子的:20030808.
在MFC中好像有这样的函数,
//CTime ltime;
//CString ls_1;
//ltime=CTime::GetCurrentTime();
//ltime -= CTimeSpan(1,0,0,0);
//ls_1=ltime.Format("%Y%m%d");
//ls_1="C:\\cfdata\\"+ls_1;
//cout<<CTime::GetCurrentTime();
我想用C++的函数,但是不知道具体的格式和函数.请大家帮助.
void gettime(struct time *timep) 本函数将计算机内的时间写入结构timep中,以供用户使用
可以这样:
char szFormated[32];
SYSTEMTIME st;
GetLocalTime(&st);
wsprintf("%d%02d%02d", st.wYear, st.wMonth, st.wDay);
修改一下最后一行,是
wsprintf(szFormated, "%d%02d%02d", st.wYear, st.wMonth, st.wDay);
/* LOCALTIM.C: This program uses time to get the current time
* and then uses localtime to convert this time to a structure
* representing the local time. The program converts the result
* from a 24-hour clock to a 12-hour clock and determines the
* proper extension (AM or PM).
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
void main( void )
{
struct tm *newtime;
char am_pm[] = "AM";
time_t long_time;
time( &long_time ); /* Get time as long integer. */
newtime = localtime( &long_time ); /* Convert to local time. */
if( newtime->tm_hour > 12 ) /* Set up extension. */
strcpy( am_pm, "PM" );
if( newtime->tm_hour > 12 ) /* Convert from 24-hour */
newtime->tm_hour -= 12; /* to 12-hour clock. */
if( newtime->tm_hour == 0 ) /*Set hour to 12 if midnight. */
newtime->tm_hour = 12;
printf( "%.19s %s\n", asctime( newtime ), am_pm );
}
Output
Tue Mar 23 11:28:17 AM
CString str;
str.Format("%.4d%.2d%.2d",your_year,your_month,your_day);
好象是这样,记不清了!!
char szTime[10];
tm time;
_getsystime(&time);
char* format = "%Y%m%d";
strftime(szTime, 10, format, &time);
兄弟,宏“__DATE__”和宏“__TIME__”就是分别指当前日期和时间的
#include <time.h>
//获取本地时间
void GetlocalTime(char *DateTime,char *LocalDate,char *LocalTime)
{
time_t timer;
struct tm *tblock;
int i;
//求当前日期及时间
timer = time(NULL);
tblock = localtime(&timer);
tblock->tm_mon++;
sprintf(DateTime,"%4d.%2d.%2d %2d:%2d:%2d",
1900+tblock->tm_year,tblock->tm_mon,tblock->tm_mday,
tblock->tm_hour,tblock->tm_min,tblock->tm_sec);
for(i=0;i<(int)strlen(DateTime);i++)
if(DateTime[i]== &&i!=10)
DateTime[i]=0;
if(LocalDate)
uf_strmid(LocalDate,DateTime,0,10); //析取日期
if(LocalTime)
uf_strmid(LocalTime,DateTime,11,8); //析取时间
}