Link to home
Start Free TrialLog in
Avatar of Julie Kurpa
Julie KurpaFlag for United States of America

asked on

VBscript does not write Midnight time to log file.

I have a vbscript that tests the status of a service every 30 minutes.  

When it executes, it writes a timestamp to a log file and a little message.

I've noticed that the midnight timestamp only writes the date and not the time.   See it below.  Why is this?

Here's the command:

objTextFile.WriteLine (now)

Here's the output:

3/7/2019 11:30:00 PM
Starting Webpage Status Check
Webpage is available.
------------------------------------------------------------------------------------
3/8/2019                                                    <-- notice only the date is written.
Starting Webpage Status Check
Webpage is available.
------------------------------------------------------------------------------------
3/8/2019 12:30:00 AM
Starting Webpage Status Check
Webpage is available.
------------------------------------------------------------------------------------
Avatar of ste5an
ste5an
Flag of Germany image

Without explicit formatting (FormatDateTime(date,format)), midnight is stripped. This is the default behavior.
Avatar of Julie Kurpa

ASKER

Ugh so picky.  :)

I googled and changed my code to the following and it writes the date OK.   3/8/2019 10:38:47 AM

Will this work at midnight?

   objTextFile.WriteLine FormatDateTime(Now(),vbGeneralDate)
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Ste5an.  :)

I'm still not getting it.   The example you give prints 1/1/2001 00:00:00 in the log file.  How can I get it to print current time even when it is midnight?

I'm trying this code which works but will it work at midnight?

objTextFile.WriteLine FormatDateTime(date, vbShortDate) & " " & FormatDateTime(now(), vbLongTime)
By using Now() instead of the date/time literal?! I took it only to show that the above works..
I think you are saying that the code is correct.  The question mark in your sentence is confusing me.
I've confirmed, the following 3 variations all print midnight as 00:00:00 just fine.  

   objTextFile.WriteLine FormatDateTime(CurrDate, vbShortDate) & " " & FormatDateTime(CurrDate, vbLongTime)
   objTextFile.WriteLine FormatDateTime(date, vbShortDate) & " " & FormatDateTime(now(), vbLongTime)
   objTextFile.WriteLine (Date & " " & time)
   objTextFile.WriteLine "Starting Webpage Status Check "

------------------------------------------------------------------------------------
3/10/2019 12:00:00 AM
3/10/2019 12:00:00 AM
3/10/2019 12:00:00 AM
Starting Webpage Status Check