The format of the current date and time in VBScript

I was wondering if anyone could help me.

I am very new to ASP. I want to format the current date and time as follows:

yyyy-mm-dd hh:mm:ss 

But all I can do is the following

 Response.Write Date 

Can someone help me.

+10
datetime vbscript asp-classic
source share
1 answer

Date formatting options are limited in Classic ASP by default, there is a function FormatDateTime() which can format your date in various ways depending on the regional settings of the server.

For more control over date formatting, although there are built-in date and time functions

  • Year(date) - Returns an integer representing the year. Passing Date() will return the current year.

  • Month(date) - Returns an integer from 1 to 12 inclusive, representing the month of the year. Passing Date() will return the current month of the year.

  • MonthName(month[, abbv]) - returns a string indicating the specified month. Going to Month(Date()) as a month will return the current month string. As suggested by @Martha

  • Day(date) - returns an integer from 1 to 31 inclusive, representing the day of the month. Passing Date() will return the current day of the month.

  • Hour(time) - Returns an integer from 0 to 23 inclusive, representing the hour of the day. Time() passing Time() will return the current hour.

  • Minute(time) - Returns an integer from 0 to 59 inclusive, representing the minute of the hour. Time() passing Time() will return the current minute.

  • Second(time) - returns an integer from 0 to 59 inclusive, representing the second of a minute. Time() passing Time() will return the current second.

IMPORTANT: When formatting date / time values, always save the date / time value first after performing any necessary calculations before trying to format, otherwise you will get unexpected results.

The Month() , Day() , Hour() , Minute() and Second() functions return integers. Fortunately, there is a simple workaround that allows you to quickly supplement these values. Right("00" & value, 2) what it does is add 00 to the front of the value, then take the first two characters to the right. This ensures that all single-valued values ​​are returned with the prefix 0 .

 Dim dd, mm, yy, hh, nn, ss Dim datevalue, timevalue, dtsnow, dtsvalue 'Store DateTimeStamp once. dtsnow = Now() 'Individual date components dd = Right("00" & Day(dtsnow), 2) mm = Right("00" & Month(dtsnow), 2) yy = Year(dtsnow) hh = Right("00" & Hour(dtsnow), 2) nn = Right("00" & Minute(dtsnow), 2) ss = Right("00" & Second(dtsnow), 2) 'Build the date string in the format yyyy-mm-dd datevalue = yy & "-" & mm & "-" & dd 'Build the time string in the format hh:mm:ss timevalue = hh & ":" & nn & ":" & ss 'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss dtsvalue = datevalue & " " & timevalue Call Response.Write(dtsvalue) 

Note: You can build a date string in one call, but decided to break it into three variables to make it easier to read.


  • How can I format the date
  • An example of parsing a date string (Answers provide approaches to obtaining the format of a date string and parsing it into a valid Date variable).
+25
source share

All Articles