Article ID: 167169
Article Last Modified on 11/21/2006
CString str = dt.Format("%x");
- or -
CString str = dt.Format("%X");
However, they only give the Date format in the US style.
...
#include "locales.h"
...
CString str;
{
CSetLocale l(LC_TIME);
str = dt.Format("%x");
str = dt.Format("%X");
}
...
If you want the date in a particular LCID, then use the following to
construct the CSetLocale object:
LCID LCIDVALUE = <set it to the required value>; CSetLocale l(LC_TIME, LCIDVALUE);This code makes sure that the runtime locale is set to the correct locale before the call to Format is made. The locale is reset to the previous locale once the CSetLocale object is destroyed.
// Locales.h: interface for the CSetLocale class.
//
//////////////////////////////////////////////////////////////////////
#if \
!defined(AFX_LOCALES_H__C6C7DD22_B292_11D0_89B4_00AA00B92B2E__INCLUDED_)
#define AFX_LOCALES_H__C6C7DD22_B292_11D0_89B4_00AA00B92B2E__INCLUDED_
#include <locale.h>
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CSetLocale
{
private:
int m_category;
CString m_lastLocale;
public:
CSetLocale(int category = LC_ALL, LCID lcid = LOCALE_USER_DEFAULT);
virtual ~CSetLocale();
};
#endif
// Locales.cpp: implementation of the CSetLocale class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Locales.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSetLocale::CSetLocale(int category, LCID lcid) : m_category(category)
{
TCHAR* buf;
CString locale;
m_lastLocale = CString(_tsetlocale(m_category, NULL));
// get the language
int len = GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE , NULL, 0);
buf = new TCHAR[len + 1];
if (!buf)
return;
buf[len] = NULL;
len = GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE , buf, len);
locale = CString(buf) + _T("_");
delete []buf;
// Get the country.
len = GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, NULL, 0);
buf = new TCHAR[len + 1];
if (!buf)
return;
buf[len] = NULL;
len = GetLocaleInfo(lcid, LOCALE_SENGCOUNTRY, buf, len);
locale +=buf;
delete []buf;
_tsetlocale(m_category, locale);
}
CSetLocale::~CSetLocale()
{
_tsetlocale(m_category, m_lastLocale);
}
COleDateTime today = COleDateTime::GetCurrentTime();
CString m_Date = today.Format(VAR_DATEVALUEONLY);
TRACE("Date : %s\n", (LPCTSTR)m_Date);
Additional query words: COleDateTime Format mfcole
Keywords: kbbug kbfix kbnoupdate kbvc600fix kbcode KB167169