Forum Moderators: open

Message Too Old, No Replies

Number formatting

How to pad numbers with extra zeros

         

mcfly

1:41 pm on Nov 10, 2004 (gmt 0)

10+ Year Member



Hi all,

I'm using ASP with integer datestamps constructed in the format YYYYMMDD for use with a database alongside dates in the proper Date format. However, converting between the two is proving awkward.

If the day or month number is a single digit, then I obviously need to pad it out to double figures with an extra zero for use in the datestamp. But ASP's formatnumber() function doesn't support this, and short of converting to a string and concatenating an extra zero I'm not sure how to proceed.

Anyone have any ideas or experience with this problem?

Cheers,

mcfly

dotme

7:43 pm on Nov 10, 2004 (gmt 0)

10+ Year Member



You're on the right track, and as luck would have it, I had to do exactly this yesterday. Here's what I wrote... Awkward, probably not the most elegant solutions, but it's short and it works.
-----------------------
tdate = Year(Date)
thismonth = cstr(month(Date))
If len(thismonth) = 1 Then
thismonth = "0" & thismonth
End If
tdate = tdate & thismonth
thisday = cstr(day(Date))
If len(thisday) = 1 Then
thisday = "0" & thisday
End If
tdate = tdate & thisday
----------------------
Code assumes server is set up for 4 digit years.

Hope this helps

mcfly

2:11 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



Thanks Dotme, that does the job perfectly.

Having learnt PHP and now transferring to ASP, I find it frustrating how ASP seems to be very limited in what I would call 'core' functions!

Cheers.

PCInk

2:47 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One way to add leading zeros (I don't know anything about .net or asp, but it's the principal that counts):

without = 6
numberofdigitswanted = 4

withzeros = right("000000000000000000000" + string(without), numberofdigitswanted)

(withzeros should now contain 0006)

txbakers

7:00 pm on Nov 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for a date I do the following:

var datint =0;
datint = year(date) * 10000 + month(date) + 100 + day(date)

not in that syntax naturally, but that's the idea.