zoukankan      html  css  js  c++  java
  • 摘:C++日期时间与字符串间的转换

     

    VC6

    CString sTime = _T("2007-10-26 13:20:30"); 

    char *charTime = (LPSTR)(LPCTSTR)sTime;

    CString sFormat = _T("%d-%d-%d %d:%d:%d");

    char *charFormat = (LPSTR)(LPCTSTR)sFormat;

    int nYear;

    int nMonth;

    int nDate;

    int nHour;

    int nMin;

    int nSec; 

    stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec);

    CTime t(nYear,nMonth,nDate,nHour,nMin,nSec);

    CString s = t.Format(_T("%Y%m%d%H%M%S"));

    VC2005

    不含中文格式

    stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec); 

    CString sTime = _T("2007-10-26 12:40:10"); 

    TCHAR *charTime = (TCHAR*)(LPCTSTR)sTime;

    CString sFormat = _T("%d-%d-%d %d:%d:%d");

    TCHAR *charFormat = (TCHAR*)(LPCTSTR)sFormat;

    int nYear;

    int nMonth;

    int nDate;

    int nHour;

    int nMin;

    int nSec; 

    _stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec);

    CTime t(nYear,nMonth,nDate,nHour,nMin,nSec);

    CString s = t.Format(_T("%Y-%m-%d %H:%M:%S"));

    含中文格式

    要包含#include <locale.h>

    CString sTime = _T("2007-10-26 12:40:10"); 

    TCHAR *charTime = (TCHAR*)(LPCTSTR)sTime;

    CString sFormat = _T("%d-%d-%d %d:%d:%d");

    TCHAR *charFormat = (TCHAR*)(LPCTSTR)sFormat;

    int nYear;

    int nMonth;

    int nDate;

    int nHour;

    int nMin;

    int nSec; 

    _stscanf(charTime,charFormat,&nYear,&nMonth,&nDate,&nHour,&nMin,&nSec);  

    CTime t(nYear,nMonth,nDate,nHour,nMin,nSec);

    _tsetlocale(LC_ALL,_T("Chinese-simplified")); //本地化

    CString s = t.Format(_T("%Y%m%d%H%M%S"));

     

  • 相关阅读:
    DNS正向解析实现
    基于bind软件部署DNS服务器
    DNS解析工具使用案例
    DNS服务工作原理
    3
    .deb/.whl/.tgz/.tar/gz软件包安装方式(Ubuntu)
    博客系列目录
    Databricks 第3篇:pyspark.sql 通过JDBC连接数据库
    Databricks 第2篇:pyspark.sql 简介
    数据预处理 第4篇:数据预处理(sklearn 插补缺失值)
  • 原文地址:https://www.cnblogs.com/shenchao/p/3208384.html
Copyright © 2011-2022 走看看