CTime与DateTimePicker控件

来源:互联网 发布:mac怎么连接vps 编辑:程序博客网 时间:2024/06/05 05:02

记录CTime与DateTimePicker控件的一些使用方法。

UI示例

DateTimePickerSample

Format

可以设置控件的Format属性,表示显示日期还是显示时间。

Format

消息

  • 对于日期控件,在显示了日期之后,会关闭。关闭对应的消息是DTN_CLOSEUP。
  • 对于时间控件,会有Spin小控件,可以上下调整。对应的消息是DTN_DATETIMECHANGE:

CTime

可以将控件映射为CTime类型的变量。

获取当前时间

用GetCurrentTime()静态成员函数:

CTime ttt = CTime::GetCurrentTime();

格式化成字符串

CTime ttt = CTime::GetCurrentTime();CString s = ttt.Format("%H:%M:%S");

获取年月日等

int theHour = ttt.GetHour();int theMinute = ttt.GetMinute();int theSecond = ttt.GetSecond();

从字符串转换成CTime

const char *buffer = "8:08:08";int theHour = 0, theMinute = 0, theSecond = 0;sscanf(buffer, "%d:%d:%d", &theHour, &theMinute, &theSecond);CTime now = CTime::GetCurrentTime();CTime sampleInterval = CTime(now.GetYear(), now.GetMonth(), now.GetDay(), theHour, theMinute, theSecond);

最好对sscanf后得到的数据进行校验,检查其是否为合法的数据。比如theHour是否介于0~23之间。否则会出现异常。

0 0
原创粉丝点击