vc 时间控件CDateTimeCtrl的使用

来源:互联网 发布:咋样做好一个淘宝店铺 编辑:程序博客网 时间:2024/06/04 19:29

strDataSource = L"2006-01-12 17:44:01";
CTime MakeDate(std::wstring strDataSource)
{
     if(strDataSource.length() < 10)
          return CTime();
     int nYear = _wtoi(strDataSource.substr(0, 4).c_str());
     int nMonth = _wtoi(strDataSource.substr(5, 2).c_str());
     int nDay = _wtoi(strDataSource.substr(8, 2).c_str());
     if(nYear < 1900 || 
            nMonth < 1 || 
            nMonth > 12 ||
            nDay < 1 || 
            nDay > 31)
            return CTime();
     return CTime(nYear, nMonth, nDay, 0, 0, 0);
}
 
CTime   t(   1999,   3,   19,   22,   15,   0   );   //   10:15PM   March   19,   1999

CTime   tm;
tm=CTime::GetCurrentTime();
m_time.SetTime(&tm);
m_time.SetFormat("yyyy-MM-dd   HH:mm:ss");

((CDateTimeCtrl*)GetDlgItem(ID_CTRL))->SetTime(&MakeDate(m_strBeginTime));

 

 

///////////////////////////////////////////////////////////

 //产生"YYYY-MM-DD hh:mm:ss"格式的字符串。
 time_t t = time(0);
 char s[32];
 strftime(s, sizeof(s),"%Y-%m-%d %H:%M:%S", localtime(&t));

 

 

 

%a

Abbreviated weekday name

%A

Full weekday name

%b

Abbreviated month name

%B

Full month name

%c

Date and time representation appropriate for locale

%d

Day of month as decimal number (01 – 31)

%H

Hour in 24-hour format (00 – 23)

%I

Hour in 12-hour format (01 – 12)

%j

Day of year as decimal number (001 – 366)

%m

Month as decimal number (01 – 12)

%M

Minute as decimal number (00 – 59)

%p

Current locale's A.M./P.M. indicator for 12-hour clock

%S

Second as decimal number (00 – 59)

%U

Week of year as decimal number, with Sunday as first day of week (00 – 53)

%w

Weekday as decimal number (0 – 6; Sunday is 0)

%W

Week of year as decimal number, with Monday as first day of week (00 – 53)

%x

Date representation for current locale

%X

Time representation for current locale

%y

Year without century, as decimal number (00 – 99)

%Y

Year with century, as decimal number

%z, %Z

Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown

%%

Percent sign

ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vccrt/html/6330ff20-4729-4c4a-82af-932915d893ea.htm

 

 

 

 

 

 

 

 

 

 

 

 

 

包含文件:
#ifndef __TIME_T
#define __TIME_T     /* 避免重复定义 time_t */
typedef long     time_t;    /* 时间值time_t 为长整型的别名*/
#endif
 
既然time_t实际上是长整型,到未来的某一天,从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间)超出了长整形所能表示的数的范围怎么办?对time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。为了能够表示更久远的时间,一些编译器厂商引入了64位甚至更长的整形数来保存日历时间。比如微软在Visual C++中采用了__time64_t数据类型来保存日历时间,并通过_time64()函数来获得日历时间(而不是通过使用32位字的time()函数),这样就可以通过该数据类型保存3001年1月1日0时0分0秒(不包括该时间点)之前的时间。
 
在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:
 
double difftime(time_t time1, time_t time0);
time_t mktime(struct tm * timeptr);
time_t time(time_t * timer);
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
 
此外,time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式tm:
 
struct tm * gmtime(const time_t *timer);                                        
struct tm * localtime(const time_t * timer);
 
通过查阅MSDN,我们可以知道Microsoft C/C++ 7.0中时间点的值(time_t对象的值)是从1899年12月31日0时0分0秒到该时间点所经过的秒数,而其它各种版本的Microsoft C/C++和所有不同版本的Visual C++都是计算的从1970年1月1日0时0分0秒到该时间点所经过的秒数。

struct tm {
  int  tm_sec;
  int  tm_min;
  int  tm_hour;
  int  tm_mday;
  int  tm_mon;
  int  tm_year;
  int  tm_wday;
  int  tm_yday;
  int  tm_isdst;
};


#include "stdafx.h"
#include <time.h>

int _tmain(int argc, _TCHAR* argv[])
{
 time_t t;
 time(&t);
 tm* tt = localtime(&t);
 int i;
 return 0;
}

原创粉丝点击