Author Topic: Realbasic 日期與 VB6日期(Double) 的互換函數  (Read 4592 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
Realbasic 日期與 VB6日期(Double) 的互換函數
« on: October 08, 2010, 12:15:31 AM »
Realbasic日期與雙精準數(VB6日期)的互換函數
Code: [Select]
Private Const cntBaseDate = "1899-12-30 00:00:00"
Function DatetoDbl(tmpDate as Date) As Double
  dim tmpResult as double
  dim tmpDateValue as integer
  dim tmpTimeValue as double
  dim tmpBaseDate as new date
 
  tmpBaseDate.SQLDateTime=cntBaseDate
 
  tmptimevalue=((tmpdate.Hour *60*60)+(tmpdate.Minute *60)+tmpdate.Second)/86400
  tmpdatevalue=(tmpdate.TotalSeconds -tmpBaseDate.TotalSeconds ) / 86400
 
  if tmpdatevalue <0 then
    tmpresult= -1
  else
    tmpresult= tmpdatevalue +tmpTimeValue
  end if
 
  return tmpResult
End Function

Function DbltoDate(tmpDouble as Double) As Date
  dim tmpResult as new Date
  dim tmpDayValue as Int64    '不可以使用Integer, 會出現溢位的計算錯誤
  dim tmpTimeValue as double
  dim tmpBaseDate as new date
  dim tmpTotalSeconds as integer
 
  tmpBaseDate.SQLDateTime=cntBaseDate
  tmpresult=tmpBaseDate
 
  tmpdayvalue=tmpdouble / 1
  tmpTimeValue=tmpDouble -tmpDayValue
 
  tmpTotalSeconds=round(tmpTimeValue * 86400)
  tmpResult.TotalSeconds=tmpResult.TotalSeconds+ (86400*tmpDayValue)+  tmpTotalSeconds
 
  return tmpResult
   
End Function

Function SQLDateTimetoDbl(tmpDateTime as string) As double
  dim tmpResult as double
  dim tmpSQLDateTime as string
  dim tmpYear as string
  dim tmpMonth as string
  dim tmpDay as string
  dim tmpTime as string
  dim d as new date
 
  if len(tmpdatetime) =17 then
    tmpyear=left(str(d.year),2)+left(tmpdatetime,2)
    tmpmonth=mid(tmpDateTime,4,2)
    tmpday=mid(tmpdatetime,7,2)
    tmpTime=right(tmpdatetime,8)
   
    tmpSQLDateTime=tmpyear +"-"+tmpmonth +"-" +tmpday +" " + tmptime
    d.SQLDateTime=tmpsqldatetime
   
    tmpResult=datetodbl(d)
   
  else
    d=new date
    tmpresult=datetodbl(d)
  end if
 
  return tmpresult
   
End Function
« Last Edit: November 16, 2011, 07:55:08 PM by Roy Chan »