13

I was wondering if someone could help me.

I'm very new at 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 out please.

Bohdan Kuts
  • 607
  • 15
  • 23
BigJobbies
  • 3,633
  • 11
  • 43
  • 66

1 Answers1

36

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

For more control over date formatting though there are built in date time functions

  • Year(date) - Returns a whole number representing the year. Passing Date() will give back the current year.

  • Month(date) - Returns a whole number between 1 and 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. Passing in Month(Date()) as the month will give back the current Month string. As suggested by @Martha

  • Day(date) - Returns a whole number between 1 and 31, inclusive, representing the day of the month. Passing Date() will return the current day of the month.

  • Hour(time) - Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Passing Time() will return the current hour.

  • Minute(time) - Returns a whole number between 0 and 59, inclusive, representing the minute of the hour. Passing Time() will return the current minute.

  • Second(time) - Returns a whole number between 0 and 59, inclusive, representing the second of the minute. Passing Time() will return the current second.

IMPORTANT: When formatting date / time values, always store the date / time value first. Also, any needed calculations (DateAdd() etc.) should be applied before attempting to format or you will get unexpected results.

The functions Month(), Day(), Hour(), Minute() and Second() all return whole numbers. Luckily there is an easy workaround that lets you pad these values quickly Right("00" & value, 2) what it does is append 00 to the front of the value then from the right take the first two characters. This ensures that all single digit values return prefixed with a 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 the date string in one call but decided to break it down into the three variables to make it easier to read.


user692942
  • 16,398
  • 7
  • 76
  • 175
  • 3
    The OP doesn't appear to need it, but there's also the MonthName(mm,abbr) function - mm is the month number, and abbr is a boolean indicating whether to abbreviate the month name or not. – Martha Mar 24 '14 at 04:00
  • @Martha Yeah, didn't include it as it wasn't relevant to the date format the OP wanted to achieve. – user692942 Mar 24 '14 at 09:46
  • 3
    You can simplify more, instead format_zeros use this other format: right("00" & month(date()),2) same format for day and year – Mastercafe Jun 26 '15 at 00:37
  • @Mastercafe That is also true. – user692942 Jun 26 '15 at 00:40
  • 2
    @ Lankymart you should use the shortcut you provided to me to revise your answer here like, tmp = Right("00" & Hour(Time()), 2). Couldn't you dump the format_zeros() function then? I appreciate you letting some of us others answer questions when I'm sure you could easily. – Jake Dec 16 '15 at 03:41
  • @Jake Yeah I need to go back and revise this answer. – user692942 Dec 16 '15 at 09:52
  • It never ceases to amaze me how ridiculous complex and faffy VBScript can manage to make even the simplest of tasks that can be accomplished with half a line of code in any sane language… – Janus Bahs Jacquet Dec 19 '18 at 15:56
  • @JanusBahsJacquet is a scripting language that is over 20 years old. – user692942 Dec 19 '18 at 15:57
  • Why does the padding need **2** zero's (as in `Right("00" & Day(dtsnow), 2)` - surely **one** zero should be enough, seen as at minimum, the day,month,hour etc will have one digit: `Right("0" & Day(dtsnow), 2)` ? – kneidels Sep 30 '21 at 06:28
  • 1
    @kneidels In that scenario `00` isn’t required but if I was passing a variable in (not `Day(dtsnow)`) and it’s value was null or empty I'd want it to default to `00`. – user692942 Sep 30 '21 at 07:04