CTime


CTime==>CString

CTime time;
time.GetCurrentTime();
CString str;
str.Format("%s",time.Format("%y:%m:%d %H-%M-%S")

1
CString str;
CTime t = CTime::GetCurrentTime();
str.Format("%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay());
2
CString strTime;
CTime tTime = CTime::GetCurrentTime();
strTime = tTime.Format("%Y-%m-%d %H:%M:%S");

CString ==>CTime

strCString="2003-10-27 6:24:37"; //CString--->COleDateTime
COleVariant vtime(strCString);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;

COleDateTime time1(1977,4,16,2,2,2); //COleDataTime--->CTime
SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);

time_t time2=tm.GetTime(); //CTime--->time_t
COleDateTime time3(time2); //time_t--->COleDateTime

時間差
COleDateTime strFirst,strEnd;
strFirst = COleDateTime(strFirst.GetYear(),strFirst.GetMonth(),strFirst.GetDay(),0,0,0);
strEnd = COleDateTime(tmCurrent.GetYear(),tmCurrent.GetMonth(),tmCurrent.GetDay(),23,59,59);
tspan= strEnd - strFirst;
注:兩個時間相減時,小時數不足24小時的,自動略去,不做一天計算。如7.1號 5點與7.2號3點間,就不能計算爲一天。想要得出結果爲一天的,要注意小時的賦值!!

CTime 使用總結

初始化      m_begintime=CTime(2004,1,1,0,0,0,-1);//參數依次爲year,month,day,hour,minite,second
 m_endtime =CTime::GetCurrentTime();//當前時間

2.日期比較

CTimeSpan span;

span=time1-time2;

得到兩時間的間隔.

可以取得span.GetHours().等

3.access數據庫查詢

使用DateDiff()函數,具體參照access幫助

CString timesql;
timesql.Format(" Where DateDiff('d',%s,'%s')<=0","日期",m_begintime.Format("%Y-%m-%d"));


4讀取日期字段(odbc)

     CDBVariant var;
      recset.GetFieldValue(i,var);
     s.Format("%d-%d-%d",(var.m_pdate)->year,(var.m_pdate)->month,
      (var.m_pdate)->day);

5.CTime轉換爲CString

   例:
    m_begintime.Format("%Y-%m-%d");//2004-10-03


6.CString轉換爲CTime

     //s="2004-10-5"
    int first=s.Find('-');
    int second=s.Find('-',first+1);

    int year=atoi(s.Left(4));
    int month=atoi(s.Mid(first+1,second-first+1));
    int day=atoi(s.Mid(second+1,s.GetLength()-second-1));
    CTime temp(year,month,day,0,0,0);


7.判斷CString是否表示的正確日期格式

//判斷是否爲2004-01-13   ch 可代表其他分隔符
bool IsDate(CString str,char ch)
{
if(str.IsEmpty()) return false;
//日期分段
int first=str.Find(ch);
int second=str.Find(ch,first+1);

int year=atoi(str.Left(4));
int month=atoi(str.Mid(first+1,second-first+1));
int day=atoi(str.Mid(second+1,str.GetLength()-second-1));
     //判斷
if (year < 2000 || year >= 2010)
{
   return false;
}
else if (month< 1 || month >12)
{
   return false;
}
else if (day< 1 || day > 31)
{
   return false;
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
   if(day > 30)
   {
    return false;
   }
   else
   {
    return true;
   }
}
else if (month == '2')
{
   if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
   {
    if (day>29)
    {
     return false;
    }
    else
    {
     return true;
    }
   }
   else if (day>28)
   {
    return false;
   }
   return true;
}
else
{
   return true;
}
     }

 

用VC編寫數據庫程序不可避免的會遇到_bstr_t 、CString 、CTime這幾個類型之間的轉換問題,令人頭疼。
今早上終於算是解決了CString和CTime的類型轉換問題。

//CTime--〉CString
CTime t;
t=CTime::GetCurrentTime();
CString sur;
sur.Format("%s",t.Format("%Y-%m-%d"));
MessageBox(sur);

//long --> CString
long a ;
CString b;
b.format("%ld",a);

//double --->CString
double a;
CString b;
b.format("%f",a);

//CString--〉CTime

CString sur;
sur="2006-08-09";
COleDateTime time1;
time1.ParseDateTime(sur);
SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);
m_time=tm;
UpdateData(false);


CString和int,float之間的轉換。

1。int <->CString

1) int ->CString

int n = 1;

CString str;

str.Format("%d",n);

2) CString->int

CString str = "1";

int n = atoi(str.GetBuffer(0));

2. char* 與CString
1)char*->CString

char sz[128];

CString str;

str.Format("%s",sz);

2) CString -> char*
CString str;

int nLength = str.GetLength();

char* sz = new char[nLength];

sz = str.GetBuffer(0);

3. float<->CString
1)float->CString
float f = 0.0;

CString str;

str.Format("%f",f);

2) CString->float
CString str = "0.0";

float f = atof(str.GetBuffer(0));


Format函數第一個參數是要轉的那個數的類型

 

FileTime 和 CTime之間的轉換問題

 

1. FileTime 轉換成 CTime
方法(1)
FILETIME ft;
CTime time(ft);

方法(2)
FILETIME ft;
SYSTEMTIME st;
BOOL bSuccess=::FileTimeToSystemTime(&ft, &st)
if (bSuccess) //轉換爲SYSTEMTIME成功,下面轉換成CTime
CTime time(st);

2. CTime 轉換成 FileTime
CTime time(CTime::GetCurrentTime());
SYSTEMTIME st;
time.GetAsSystemTime(st);
FILETIME ft;
::SystemTimeToFileTime(&st, &ft

發佈了23 篇原創文章 · 獲贊 4 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章