Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date & Time - Saved in file name 1

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi all,

Could someone show me the best way to get the date and time (24 clock) to display like this,

Code:
090308143000
[code]

assume the date and time was the 9th (09) of march (03) 2008 (08) at the time 2 (14) 30 (30) PM and zero seconds (00).

I'd like to save that string within a file name so that it is not only unique but also it lets me know the exact date/time it was created.

Thanks.
 
Have a look at the Date and Time functions available in VBScript. You will need Day(), Month(), Year(), Hour(), Minute() and Second(). There is a list, along with examples here:
You will need to use some string manipulation to get the parts into 2 digit strings before you concatenate them. The string functions are on the same page. You will need the Right() function after concatenating "0" on to the front of each part of your answer.

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
thanks for that Johnwm,

I ended up with the following code

Code:
<%
Function gDate(dt)
Dim md,mm,my,tm,sm
ms=Second(dt)
If Len(ms)=1 Then ms="0" & ms
md=Day(dt)
If Len(md)=1 Then md="0" & md
mm=Month(dt)
If Len(mm)=1 Then mm="0" & mm
my=Year(dt)
gDate=md & mm & Right(my,2)& Hour(dt) & Minute(dt) & ms
End Function
%>
<%=gDate(NOW())%>

incase any is helped by it, or can see a better way (a reason it will error)


Thanks.
 
To save the Ifs and the repetitive coding you could do a 'lengthen and trim' function.
Code:
Function make2(input)
make2=Right("00" & Cstr(input),2)
End Function
That just takes your day/min/second/year/whatever, adds "00" to the front then takes the rightmost 2 characters, so make2(2008) returns 08 and make2(1) returns 01. Just use make2() on every one of the Day(), Year() etc to save all the Ifs

___________________________________________________________
If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints
 
Great, I'll add that now, Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top