Solved

Need VBS version of Excel date conversion

Posted on 2016-08-30
4
53 Views
Last Modified: 2016-08-31
I have this in Excel but need a vbscript version to run on CMD line, i.e. cscript dateconvert.vbs 1255635089

A5=1255635089
B5=A5/86400+DATE(1970,1,1)-(10/24)

I have this so far...

BTW, I'm not an expert VBS coder as you can see. So, I'm open to better / cleaner solution.

EpochToDate.vbs
Set objArgs = WScript.Arguments
nEpochVal = objArgs(0)
Dim dblVbEpoch
    dblVbEpoch = CDbl(DateAdd("s", nEpochVal, #1970/1/1#))

sTime=FormatDateTime(DateAdd("n", CurrentTZOffset(), CDate(dblVbEpoch)),3)
sTime=Replace(sTime,":","_")
sTime=Replace(sTime," ","")
WScript.Echo myDateFormat(DateAdd("n", CurrentTZOffset(), CDate(dblVbEpoch))) & " " & sTime

Function CurrentTZOffset()
    With CreateObject("WScript.Shell") 
        CurrentTZOffset = - .RegRead( _ 
        "HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
    End With
End Function

Function myDateFormat(myDate)
    d = WhatEver(Day(myDate))
    m = WhatEver(Month(myDate))    
    y = Year(myDate)
    myDateFormat= y & "_" & m & "_" & d
End Function

Function WhatEver(num)
    If(Len(num)=1) Then
        WhatEver="0"&num
    Else
        WhatEver=num
    End If
End Function

Open in new window


But, running cscript /nologo EpochToDate.vbs 1255635089
... echos it as: 2009_10_15 9_31_29AM
... which is fine except:

- I don't want the AM or PM suffix
- If the hour is a single digit, I'd like it prefixed with a zero. This, for sorting reasons.
0
Comment
Question by:NVIT
  • 2
4 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 41777486
Set fso = CreateObject ("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
nEpochVal = objArgs(0)
Dim dblVbEpoch
Dim Mydate
dblVbEpoch = CDbl(DateAdd("s", nEpochVal, #1970/1/1#))
Mydate = DateAdd("n", CurrentTZOffset(), CDate(dblVbEpoch))
Mydate = Replace(Mydate,"-","_")
Mydate = Replace(Mydate," ","_")
Mydate = Replace(Mydate,":","_")
WScript.echo(Mydate)

Function CurrentTZOffset()
    With CreateObject("WScript.Shell") 
        CurrentTZOffset = - .RegRead( _ 
        "HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
    End With
End Function

Open in new window


08_Dec_2009_6_17_30_PM  (I am in EDT5EST and my regional settings are set to dd-MMM-YYYY
0
 
LVL 23

Author Comment

by:NVIT
ID: 41777570
David,

I haven't had a chance to try your solution. I assume your result 08_Dec_2009_6_17_30_PM is based on it.

Still...
- I don't want the AM or PM suffix.
- If the hour is a single digit, I'd like it prefixed with a zero.

To clarify, 2009_12_08_06_17_30_PM should show (localized) as 2009_12_08_18_17_30
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41777920
How about this:

If (Wscript.Arguments.Count > 0) Then
  nEpochVal = Wscript.Arguments(0)
Else
  WScript.Echo "ERROR : No input value specified."
  WScript.Quit -1
End If

dblVbEpoch = CDbl(DateAdd("s", nEpochVal, #1970/1/1#))

WScript.Echo myDateTimeFormat(DateAdd("n", CurrentTZOffset(), CDate(dblVbEpoch)))

Function CurrentTZOffset()
    With CreateObject("WScript.Shell") 
        CurrentTZOffset = - .RegRead( _ 
        "HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
    End With
End Function

Function myDateTimeFormat(myDate)
    myDateTimeFormat= Year(myDate) & "_" & _
                      ZPad(Month(myDate), 2) & "_" & _
                      ZPad(Day(myDate), 2) & " " & _
                      ZPad(Hour(myDate), 2) & "_" & _
                      ZPad(Minute(myDate), 2) & "_" & _
                      ZPad(Second(myDate), 2)
End Function

Function ZPad(strText, intLen)
   ' Pad with leading zeros on the left to specified length
   ZPad = Right(String(intLen, "0") & strText, intLen)
End Function

Open in new window

~bp
0
 
LVL 23

Author Closing Comment

by:NVIT
ID: 41778961
Thanks, Bill!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
My experience with Windows 10 over a one year period and suggestions for smooth operation
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now