Public Function Frac(v)
	Frac = v - Fix(v)
End Function

Public Function NextHour(d)
	d = cDate(d)
	NextHour = Fix(d * 24 + 1) / 24
End Function

Public Function NextHalfHour(d) 
	d = cDate(d)
	NextHalfHour = Fix(d * 48 + 1) / 48
End Function


Public Function NextHalfHourTime(d)
	d = cDate(d)
	NextHalfHourTime = CDate(Frac(NextHalfHour(d)))
End Function

Public Function NextHalfHourDate(d)
	d = cDate(d)
	NextHalfHourDate = CDate(Fix(NextHalfHour(d)))
End Function


Public Function ToUniversalDate(d)
	Dim dt
	Dim t

	err.Clear
	on error resume next

	dt = Fix(cdate(d))
	t = Frac(cdate(d))

	dt = Year(dt) & "-" & Month(dt) & "-" & Day(dt)

	if err then
		ToUniversalDate = ""
	else

		if t <> 0 then
			ToUniversalDate = dt & " " & t
		else
			ToUniversalDate = dt
		end if
		
	end if
	
	on error goto 0
	
End Function

Public Function FromUniversalDateVB(d)
	msgbox IsDate(d)
	FromUniversalDate = FormatDateTime(d,vbShortDate)
End Function


Public Function vbToUniversalFormat(dt)
	Dim sReturn
	Dim iMinute
	Dim iSecond
	Dim iHour
	Dim iDay
	Dim iMonth
	Dim sHourText
	
	iMinute = cStr(Minute(dt))
	if len(iMinute) = 1 then iMinute = "0" & iMinute
	
	iSecond = cStr(Second(dt))
	if len(iSecond) = 1 then iSecond = "0" & iSecond
	
	iDay = cStr(Day(dt))
	if len(iDay) = 1 then iDay = "0" & iDay
	
	iMonth = cStr(Month(dt))
	if len(iMonth) = 1 then iMonth = "0" & iMonth
	
	iHour = Hour(dt)

    if iHour < 12 then
        sHourText = "AM"
    else
        sHourText = "PM"
    end if   

    if iHour > 12 then iHour = iHour - 12
    if iHour = 0 then iHour = 12
    	
	iHour = Cstr(iHour)
	if len(iHour) = 1 then iHour = "0" & iHour
	
	sReturn = Year(dt) & "-" & iMonth & "-" & iDay & " " & iHour & ":" & iMinute & ":" & iSecond & " " & sHourText

	vbToUniversalFormat = sReturn
End Function


Public function vbIsDateWrapper(sDate)
	vbIsDateWrapper = IsDate(sDate)
End function

public function vbCDateWrapper(sDate)
	if IsDate(sDate) then
		vbCDateWrapper = cDate(sDate)
	else
		vbCDateWrapper = ""
	end if
end function


public function vbCDateUniversalFormatWrapper(sDate)
	if IsDate(sDate) then
		Dim dt 
		dt = cDate(sDate)
		
		vbCDateUniversalFormatWrapper = vbToUniversalFormat(dt)
	else
		vbCDateUniversalFormatWrapper = ""
	end if
end function

public function vbDateDiffWrapper(dtDate1,dtDate2,sFormat)
    If not isDate(dtDate1) or not isDate(dtDate2) Then
        vbDateDiffWrapper = 0
        Exit Function
    End If
    
    dtDate1 = CDate(dtDate1)
    dtDate2 = CDate(dtDate2)
    
    Select Case sFormat
        Case "YEAR":
		    vbDateDiffWrapper = DateDiff("y",dtDate1,dtDate2)
        Case "MONTH":
		    vbDateDiffWrapper = DateDiff("m",dtDate1,dtDate2)
        Case "DAYS":
		    vbDateDiffWrapper = DateDiff("d",dtDate1,dtDate2)
		Case "HOURS":
		    vbDateDiffWrapper = DateDiff("h",dtDate1,dtDate2)
		Case "MINUTES":
		    vbDateDiffWrapper = DateDiff("n",dtDate1,dtDate2)
		Case "SECONDS":
		    vbDateDiffWrapper = DateDiff("s",dtDate1,dtDate2)
		Case Else:
		    vbDateDiffWrapper = 0
	End Select 
end function

public function vbDateAddWrapper(interval, number, sDate)
	Dim dtDate
	dtDate = cdate(sDate)
	
	dtDate = DateAdd(interval, number, dtDate)
	
	vbDateAddWrapper =  vbToUniversalFormat(dtDate) 
end function

public function vbShortDateWrapper(sDate)
	dim dtDate
	dtDate = cdate(sDate)
	vbShortDateWrapper = FormatDateTime(dtDate, vbShortDate)
end function