| 
| 
| 
| #196767 - 2009-11-19 02:07 PM  (Not) Fun with the ScriptControl object |  
| Skatterbrainz   Starting to like KiXtart
 
   Registered:  2002-10-17
 Posts: 172
 Loc:  Virginia, USA
 | 
I'm curious what is causing this to not work in 4.61.  The DateDiff function works, but DateAdd and DatePart don't return any result.  Maybe I'm doing something wrong?
 
 
Function DateDiff($unit, $date1, $date2)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval("DateDiff("+Chr(34)+$unit+Chr(34)+", "+Chr(34)+$date1+Chr(34)+", "+Chr(34)+$date2+Chr(34)+")")
    $sc = 0
    $DateDiff = $result
EndFunction
Function DateAdd($unit, $incr, $date)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval("DateAdd("+Chr(34)+$unit+Chr(34) + ", " + $incr + ", " + Chr(34)+$date+Chr(34)+")")
    $sc = 0
    $DateAdd = $result
EndFunction
Function DatePart($unit, $date)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval("DatePart("+Chr(34)+$unit+Chr(34)+", "+Chr(34)+$date+Chr(34)+")")
    $sc = 0
    $DatePart = $result
EndFunction
$testvalue = '2009/11/03 12:34:56'
$test1 = FormatDateTime($testvalue, 'vbShortDate')
$test2 = FormatDateTime($testvalue, 'vbLongDate')
$test3 = FormatDateTime($testvalue, 'vbLongTime')
$test4 = DateDiff('d', '7/19/2009', $testvalue)
$test5 = DateAdd('m', 1, $testvalue)
$test6 = DatePart('m', $testvalue)
? "shortdate...: $test1"
? "longdate....: $test2"
? "longtime....: $test3"
? "datediff....: $test4"
? "dateadd.....: $test5"
? "datepart....: $test6"
when I run this, variables $test5 and $test6 contain no value.  My computer is running Windows XP Pro SP3, using KiXtart 4.61 final release.
 
 Edited by Skatterbrainz (2009-11-19 02:08 PM)
 
_________________________silence is golden, but duct tape is silver
 |  
| Top |  |  |  |  
| 
| 
| #196772 - 2009-11-19 05:14 PM  Re: (Not) Fun with the ScriptControl object
[Re:  Skatterbrainz] |  
| Richard H.   Administrator
 
       
 Registered:  2000-01-24
 Posts: 4946
 Loc:  Leatherhead, Surrey, UK
 | 
It's because the date returned is a complex data type.
 If you cast it to a simple data type (using CStr() in this example) then it'll come back in a usable form.
 
 I've added a simple FormatDateTime(), as you missed it in your post.  I've also collapsed the Chr()s, as they were giving me a headache
  
 Function FormatDateTime($v, $f)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval('FormatDateTime("'+$v+'",'+$f+')')
    $sc = 0
    $FormatDateTime = $result
EndFunction
Function DateDiff($unit, $date1, $date2)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval('DateDiff("'+$unit+'", "'+$date1+'", "'+$date2+'")')
    $sc = 0
    $DateDiff = $result
EndFunction
Function DateAdd($unit, $incr, $date)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval('CStr(DateAdd("'+$unit+'", ' + $incr + ', "'+$date+'"))')
    $sc = 0
    $DateAdd = $result
EndFunction
Function DatePart($unit, $date)
    Dim $sc, $result
    $sc = CreateObject("ScriptControl")
    $sc.Language = "vbscript"
    $result = $sc.Eval('CStr(DatePart("'+$unit+'", "'+$date+'"))')
    $sc = 0
    $DatePart = $result
EndFunction
$testvalue = '2009/11/03 12:34:56'
$test1 = FormatDateTime($testvalue, 'vbShortDate')
$test2 = FormatDateTime($testvalue, 'vbLongDate')
$test3 = FormatDateTime($testvalue, 'vbLongTime')
$test4 = DateDiff('d', '7/19/2009', $testvalue)
$test5 = DateAdd('m', 1, $testvalue)
$test6 = DatePart('m', $testvalue)
? "shortdate...: $test1"
? "longdate....: $test2"
? "longtime....: $test3"
? "datediff....: $test4"
? "dateadd.....: $test5"
? "datepart....: $test6"
 This gives me:
 
 shortdate...: 11/3/2009longdate....: Tuesday, November 03, 2009
 longtime....: 12:34:56 PM
 datediff....: 107
 dateadd.....: 12/3/2009 12:34:56 PM
 datepart....: 11
 Which is what you want, I think.
 |  
| Top |  |  |  |  
 Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
 
 | 
| 
 
| 0 registered
and 739 anonymous users online. 
 | 
 |  |