Methods for formatting output dates in VBScript

Source: Internet
Author: User
Tags numeric value

In using VBScript, you often need to use the format output date method and summarize it as follows:


1. using the formatdatetime function

This function returns an expression that has been formatted as a date or time.

The FormatDateTime function uses the format formatdatetime (date[, NamedFormat])

which

Date is the required option. The date expression to be formatted.

NamedFormat is available as an option. Indicates the numeric value of the date/time format used, and if omitted, uses Vbgeneraldate.

The NamedFormat parameter can have the following values:

Constants

Value

Description

Vbgeneraldate

0

Displays the date and/or time. If there is a date part, the section is displayed as a short date format. If there is a time section, the section is displayed as a long-time format. If they all exist, all sections are displayed.

vbLongDate

1

Displays the date using the long date format specified in the computer's regional settings.

Vbshortdate

2

Displays the date using the short date format specified in the computer's regional settings.

Vblongtime

3

Displays the time using the time format specified in the computer's regional settings.

vbShortTime

4

Displays the time using the 24-hour format (hh:mm).

2. custom output, such as the year 9 months 9 days output to 20100909

But the date format feature that VBScript comes with does not support a two-digit date format, if you use the following code

Strdate= CStr (now ()) &cstr (Month (now ())) &cstr (today (now)))

Then the character in Strdate is 201099

There are several ways to solve this problem:

1) Write a handler function, for example, called Get2digits

If Len (now) =1 then month= "0" &month

This function is called every time.

2) We can also get: Right ("0" &month (now), 2), the function is actually very simple, the first force of the month before the character "0", so that the format becomes 06, 08, 012, and then take the two characters to the left, that is 06, 08 , 12. This is both brief and avoids the problem that too many if judgment statements cause program execution to slow down. This method is obviously much better. Then you can change the

Strdate=cstr (now ()) &right ("0" &month (now ()), 2) &right ("0" &day (now ()), 2)

3) Now () the format of the output is 2010/09/09 12:47:59

So we can also take this way to format the output date

Strdate=left (Now (), 4) &mid (now (), 6,2) &mid (now (), 9,2)

But, with a little attention, I tested it in the Japanese OS, as the result of now () is 2010/09/09 12:47:59 in this format, but in the English OS The result is 09/09/2010 12:47:59, This is the method to change the above, this should be very careful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.