Wednesday, March 7, 2012

Date format in ActiveX

How do I get the date/time in the following format:
2003-02-26 04:25:14
or
2003-02-26 14:25:14
How do I get this format using only ActiveX commands?
Thanks in advance.there is a way to change your regional settings with win api (it's been a while, I can't remember what I was bringing into vb to talk to that applet).|||You can use VB's Format command with the pre-defined date/time string like "GeneralDate" or something. It is based on the system's LocalID to provide localized time formats - or - if you want the date this way permanently you can "roll your own" formatting...

format(dtTargetDateTime, "YYYY-MM-DD hh:mm:ss")|||Originally posted by bill_dev
You can use VB's Format command with the pre-defined date/time string like "GeneralDate" or something. It is based on the system's LocalID to provide localized time formats - or - if you want the date this way permanently you can "roll your own" formatting...

format(dtTargetDateTime, "YYYY-MM-DD hh:mm:ss")


Well the purpose of this is because I want to write the date to a file while a DTS package is running, ie, some sort of logging. But I'm doing the loggin manually using AcitveX script in the DTS package.

Now the problem is I want the Date in the above format, but the ActiveX script available in the DTS package tasks does not have the 'Format()' function.

Using the codes available in the DTS ActiveX script tasks is it possible to write the date/time in the above format?

Thanks,|||Yeah, VBSCRIPT only has the FormatDateTime() function. You can use it with the predefined formats I mentioned above or build your own formatting like this:

sDate = year(dt) & "-" & month(dt) & "-" & day(dt) & _
" " & formatdatetime(dt,vbLongTime)

...not sure what the longtime constant is. You'd need to look it up. Hope that helps.|||Originally posted by bill_dev
Yeah, VBSCRIPT only has the FormatDateTime() function. You can use it with the predefined formats I mentioned above or build your own formatting like this:

sDate = year(dt) & "-" & month(dt) & "-" & day(dt) & _
" " & formatdatetime(dt,vbLongTime)

...not sure what the longtime constant is. You'd need to look it up. Hope that helps.

Thanks for the tip.

I ended up using a longer method that works.

Dim datetime
datetime = Year(Date) & "-"
If Month(Date) < 10 Then
datetime = datetime & "0"
End If
datetime = datetime & Month(Date) & "-"
If Day(Date) < 10 Then
datetime = datetime & "0"
End If
datetime = datetime & Day(Date) & " "
If Hour(Time) < 10 Then
datetime = datetime & "0"
End If
datetime = datetime & Hour(Time) & ":"
If Minute(Time) < 10 Then
datetime = datetime & "0"
End If
datetime = datetime & Minute(Time) & ":"
If Second(Time) < 10 Then
datetime = datetime & "0"
End If
datetime = datetime & Second(Time)

Thanks again.|||Great. That's just an example, right? You'd want different variable names in the real thing...

No comments:

Post a Comment