简单问题,获取系统日期,然后做个转换。谢谢。

yztas 2012-01-19 11:25:05
请教一下,
获得系统日期 ,然后转换成十六进制的BYTE类型。
例如:系统日期为 2011年6月15日。
转换为
BYTE date[8]= { 0x02,0x00,0x01,0x01,
0x00,0x06,0x01,0x05,}


大家给指导指导啊。给个代码吧,非常感谢啊。
...全文
171 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiuzhoulh 2012-01-19
  • 打赏
  • 举报
回复
SYSTEMTIME *systime = new SYSTEMTIME;
GetLocalTime(systime);
int years ;
int months ;
int days ;
int hours ;
int minutes ;
int seconds ;
int milliseconds ;
years = systime->wYear ;
months = systime->wMonth ;
days = systime->wDay ;
hours = systime->wHour ;
minutes = systime->wMinute ;
seconds = systime->wSecond ;
milliseconds = systime->wMilliseconds ;

delete systime ;
systime = NULL ;

CString str_time ;
str_time.Format("%d-%d-%d %d:%d:%d:%d" ,
years ,months ,days ,hours ,minutes ,seconds ,milliseconds);
fronz 2012-01-19
  • 打赏
  • 举报
回复
补充一下,
lpSystemTime.wYear;
lpSystemTime.wMonth;
lpSystemTime.wDay;

提出之后可分别处理。
yztas 2012-01-19
  • 打赏
  • 举报
回复
VisualEleven老大 能不能完整一点,嘿嘿。拜谢。
Eleven 2012-01-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yztas 的回复:]
请教一下VisualEleven老大,不用mfc的,CTime用什么替换
[/Quote]
void WINAPI GetSystemTime(
__out LPSYSTEMTIME lpSystemTime
);
ribut9225 2012-01-19
  • 打赏
  • 举报
回复
偶是用WIN API的,加头文件window.h
ribut9225 2012-01-19
  • 打赏
  • 举报
回复
写一个不用MFC,新手,大家不要笑话
SYSTEMTIME stTime; 
BYTE date[8];
char temp[4];
char t;
 
GetLocalTime(&stTime);   
int wYear = stTime.wYear;   
int wMonth = stTime.wMonth;   
int wDay= stTime.wDay;   ;

itoa(wYear,temp,10)

date[0] = temp[0] -30 //年份第一位
date[1] = temp[1];
date[2] = temp[2];
date[3] = temp[3]; //年份第四位

itoa(wMonth,temp,10);
if(wMonth<10)
{
date[4] = 0;
date[5] = temp[0] - 30;
}
else
{
date[4] = temp[0] - 30;
date[5] = temp[1] - 30;
}

itoa(wDay,temp,10)
if(wDay<10)
{
date[6] = 0;
date[7] = temp[0] - 30;
}
else
{
date[6] = temp[0] - 30;
date[7] = temp[1] - 30;
}
yztas 2012-01-19
  • 打赏
  • 举报
回复
请教一下VisualEleven老大,不用mfc的,CTime用什么替换
yztas 2012-01-19
  • 打赏
  • 举报
回复
感谢大家指导啊,特别是VisualEleven老大。 但是不想用mfc的。
hsLi001 2012-01-19
  • 打赏
  • 举报
回复
事实上
BYTE date[8]= { 0x02,0x00,0x01,0x01,
0x00,0x06,0x01,0x05,}

BYTE date[8]= { 2,0,1,1,
0,6,1,5,}是一样的,又不是字符串
所以你获取时间后直接复制就可以了:
date[0] = time.GetYear()/1000;
...
huziwu 2012-01-19
  • 打赏
  • 举报
回复
CTime time = CTime::GetCurrentTime();
time.GetYear()//年
time.GetMonth()//月
time.GetDay()//日
至于怎么赋给Date[8]就不用讲了吧。
Eleven 2012-01-19
  • 打赏
  • 举报
回复
用字符串来转比较好

CTime time = CTime::GetCurrentTime();
CString strText;
strText.Format(_T("%04d%02d%02d"), time.GetYear(), time.GetMonth(), time.GetDay());
int len = strText.GetLength();
BYTE* pData = new BYTE[len];
memset(pData, 0, len * sizeof(BYTE));
for(int i=0; i<len; i++)
{
pData[i] = strText[i] - 0x30;
}

CString str;
CString tmp;
for(int i=0; i<len; i++)
{
tmp.Format(_T("0x%02x, "), pData[i]);
str += tmp;
}
AfxMessageBox(str);
zero_226 2012-01-19
  • 打赏
  • 举报
回复
获取系统时间 GetLocalTime
转换,不会。。。。。。。
Eleven 2012-01-19
  • 打赏
  • 举报
回复
#include "stdafx.h"

#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
SYSTEMTIME systime;
GetLocalTime(&systime);
TCHAR szBuf[10] = {0};
_stprintf_s(szBuf, _countof(szBuf), _T("%04d%02d%02d"), systime.wYear, systime.wMonth, systime.wDay);
int len = _tcslen(szBuf);
BYTE* pData = new BYTE[len];
memset(pData, 0, len * sizeof(BYTE));
for(int i=0; i<len; i++)
{
pData[i] = szBuf[i] - 0x30;
}

TCHAR buf[256] = {0};
TCHAR tmp[10] = {0};
for(int i=0; i<len; i++)
{
_stprintf_s(tmp, _countof(tmp), _T("0x%02x, "), pData[i]);
_tcscat_s(buf, _countof(buf) - _tcslen(buf), tmp);
}
_tprintf(_T("%s\n"), buf);
return 0;
}
yztas 2012-01-19
  • 打赏
  • 举报
回复
综合大家的,下面这样对不对啊?再次谢谢大家指导啊。

#include "stdafx.h"

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//CTime time = CTime::GetCurrentTime();


SYSTEMTIME systime;
GetLocalTime(&systime);
CString strText;
strText.Format(_T("%04d%02d%02d"), systime.wYear, systime.wMonth, systime.wDay);
int len = strText.GetLength();
BYTE* pData = new BYTE[len];
memset(pData, 0, len * sizeof(BYTE));
for(int i=0; i<len; i++)
{
pData[i] = strText[i] - 0x30;
}

CString str;
CString tmp;
for(int i=0; i<len; i++)
{
tmp.Format(_T("0x%02x, "), pData[i]);
str += tmp;
}
cout<<str<<endl;
return 0;
}




16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧