Home > Software engineering >  Visual Basic BGInfo Script to change date format to YYYY-MM-DD
Visual Basic BGInfo Script to change date format to YYYY-MM-DD

Time:12-15

I use this code to change the date format for BGInfo but I get error code on line 3

Mismatch error 'Format" 800A000D

Dim dt, strDate
dt = Date
strDate = Format(dt, "yyyy-mm-dd")
BGInfo.BGI.Date = strDate 

Research I've done doesn't seen to find a solution

I've tried this:

 Dim strDate As String
 strDate = Format(Now(), "yyyy/mm/dd")
 BGInfo.BGI.Date = strDate

but that didn't work

CodePudding user response:

BGInfo uses VBScript in it's Custom field VB Script file option. It does not support VBA, so VBA functions, such as Format cannot be used.

The Following code can be placed in the Custom field to provide a new field, that can be placed on screen, to display the date in yyyy-mm-dd format:

Echo Year(Now()) & "-" & Right("0" & Month(Now()),2) & "-" & Right("0" & Day(Now()),2)

However, this will not change the format of the existing fields, such as Boot Time.

There is no bginfo object, so the code BGInfo.BGI.Date = strDate will not work. The only way to change the date format for bginfo's built-in fields is to change the user's date format.

You can work around the issue by temporarily changing the user's date format to yyyy-mm-dd, running bginfo, and then change the date format back. Here's the code (save this to a separate file, such as RunBGInfo.vbs):

Set oWSH = CreateObject("Wscript.Shell")
sDateReg = "HKCU\Control Panel\International\sShortDate"
UserDateFormat = oWSH.RegRead(sDateReg)
oWSH.RegWrite(sDateReg),"yyyy-MM-dd"
oWSH.Run "bginfo.exe bginfo.bgi /timer:0 ",1,True
oWSH.RegWrite(sDateReg),UserDateFormat

You may also want to look at a bginfo alternative written in PowerShell (such as this one) that you can then modify and extend to meet your requirements.

  • Related