datetime - Format current date and time -
i wondering if me.
i'm new @ asp want format current date , time follows:
yyyy-mm-dd hh:mm:ss
but can following
response.write date
can me out please.
date formatting options limited in classic asp default, there function formatdatetime()
can format date various ways based on servers regional settings.
for more control on date formatting though there built in date time functions
year(date)
- returns whole number representing year. passingdate()
give current year.month(date)
- returns whole number between 1 , 12, inclusive, representing month of year. passingdate()
return current month of year.monthname(month[, abbv])
- returns string indicating specified month. passing inmonth(date())
month give current month string. as suggested @marthaday(date)
- returns whole number between 1 , 31, inclusive, representing day of month. passingdate()
return current day of month.hour(time)
- returns whole number between 0 , 23, inclusive, representing hour of day. passingtime()
return current hour.minute(time)
- returns whole number between 0 , 59, inclusive, representing minute of hour. passingtime()
return current minute.second(time)
- returns whole number between 0 , 59, inclusive, representing second of minute. passingtime()
return current second.
the functions month()
, day()
, hour()
, minute()
, second()
return whole numbers. luckily there easy workaround lets pad these values right("00" & value, 2)
append 00
front of value right take first 2 characters. ensures single digit values return prefixed 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 date string in format yyyy-mm-dd datevalue = yy & "-" & mm & "-" & dd 'build time string in format hh:mm:ss timevalue = hh & ":" & nn & ":" & ss 'concatenate both build timestamp yyyy-mm-dd hh:mm:ss dtsvalue = datevalue & " " & timevalue call response.write(dtsvalue)
note: you can build date string in 1 call decided break down 3 variables make easier read.
Comments
Post a Comment