日期时间验证截止到2012年7月25日15:08:23
DateTime Validation as 25-Jul-2012 15:08:23
我使用下面的方法来验证Date。如何格式化字符串中的月份?
bool CDateTime :: IsValidDate(char* pcDate) //pcDate = 25-Jul-2012 15:08:23
{
bool bVal = true;
int iRet = 0;
struct tm tmNewTime;
iRet = sscanf_s(pcDate, "%d-%d-%d %d:%d:%d", &tmNewTime.tm_mon, &tmNewTime.tm_mday, &tmNewTime.tm_year, &tmNewTime.tm_hour, &tmNewTime.tm_min, &tmNewTime.tm_sec);
if (iRet == -1)
bVal = false;
if (bVal == true)
{
tmNewTime.tm_year -= 1900;
tmNewTime.tm_mon -= 1;
bVal = IsValidTm(&tmNewTime);
}
return bVal;
}
使用strptime:
#include <time.h>
char *str = "25-Jul-2012 15:08:23";
struct tm tm;
if (strptime (str, "%d-%b-%Y %H:%M:%S", &tm) == NULL) {
/* Bad format !! */
}
C++11实现这一点的方法是:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
int main()
{
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
std::cout << "Now is " << std::put_time(std::localtime(&now_c), "%d-%b-%Y %H:%M:%S") << 'n';
}
注意:流I/O操纵器std::put_time
尚未在所有编译器中完全实现。例如,GCC 4.7.1没有。
相关文章:
- 输入日,月,年并存储在单独的结构中
- 如何从C++中的字符串ID中提取int日,月和年
- 如何将字符串(2019年10月3日星期四18:45:10)转换为cpp中的time_t格式?
- 用于在C++中将串行日期 (Excel) 转换为年-月-日的算法
- 仅将纪元字符串保存到 mysql 日期时间年/月/日,但没有小时分钟秒
- 如何检查日期格式(日/月/年)是否正确C++
- 从1970年1月1日起,C 设置了MS的Windows时间
- 根据用户输入生成年、月、周和日
- c++time_t返回到1970年1月1日之前
- C 检索自1970年1月1日以来的MS数量:无法开源文件SYS/TIME.H
- 日蚀月神与明格:"Launch failed. Binary not found."
- 如何使用Visual Studio 2013 CTP 2013年11月11日建立Boost 1.55
- 如何在 C 或 C++ 中比较日/月/年
- 这是Zeller程序的代码.除了1111年2月31日、1111年6月31日这一通常是错误的日期外,一切都很顺利
- 日期时间验证截止到2012年7月25日15:08:23
- 从给定日期提取日、月、年
- C++ 如何从一个整数值中获取三个单独的日期值(日、月、年)
- 日期格式操作(年/月/日到月/日/年)
- c++获取用户选择的日期与实际日期之间的年份(计算日、月、年)
- 在C中使用Gettimeofday()的日、月、年算法