VBscript does not write Midnight time to log file.

Julie Kurpa
Julie Kurpa used Ask the Experts™
on
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.
------------------------------------------------------------------------------------
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
Without explicit formatting (FormatDateTime(date,format)), midnight is stripped. This is the default behavior.
Julie KurpaSr. Systems Programmer

Author

Commented:
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)
Senior Developer
Commented:
No, cause the date in the constant indicates a date focused format, thus midnight is stripped. Thus it's getting a little bit ugly here:

 objTextFile.WriteLine FormatDateTime(#1/1/2001 00:00:00#, vbShortDate) & " " & FormatDateTime(#1/1/2001 00:00:00#, vbShortTime) 

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Julie KurpaSr. Systems Programmer

Author

Commented:
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)
ste5anSenior Developer

Commented:
By using Now() instead of the date/time literal?! I took it only to show that the above works..
Julie KurpaSr. Systems Programmer

Author

Commented:
I think you are saying that the code is correct.  The question mark in your sentence is confusing me.
Julie KurpaSr. Systems Programmer

Author

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial