2014-04-17 80 views
1

好吧,所以我的代碼工作正常,但我需要它添加一天到OrderDate,同時保持它在相同的格式yyyy-mm-dd任何幫助極大讚賞!我需要添加一天到我的日期在我的代碼,試過DateAdd

<% 
inDate = oRS("ordDate") 

' get each date component 
thisMonth = datepart("M",inDate) 
thisDay = datepart("D",inDate) 
thisYear = datepart("yyyy",inDate) 

' check for the length of the month and day components 
if len(thisMonth) < 2 then thisMonth = "0" & thisMonth 
if len(thisDay) < 2 then thisDay = "0" & thisDay 

' Create the date in your format you posted above 
OrderDate = thisYear & "-" & thisMonth & "-" & thisDay 
%> 

回答

0

試試這個:

dim sOrderDate 
sOrderDate=cInt(thisDay)+1 

if len(thisDay) < 2 then thisDay = "0" & thisDay 
if len(sOrderDate) < 2 then sOrderDate = "0" & sOrderDate 

OrderDate = thisYear & "-" & thisMonth & "-" & sOrderDate 

這樣您保留了操縱和訂單日期原始日期(THISDAY)成爲明日(或其他)的日期。

+2

我回來刪除的問題,我可以只使用:\t \t THISDAY =日期部分(「d」,使用DateAdd(「d」,1,銦酸)) – BWalt302

+0

,將工作,但您可能希望保留記錄的原始日期。特別是如果你處理金錢交易。 –

+0

此代碼的目的只是爲了生成預計發貨日期,這是大多數情況下總是在訂單後1天,有時是同一天。但是原始日期仍然可以在訂單表中找到。謝謝您的幫助!我繼續選擇你的答案作爲答案。 – BWalt302

1

以您的代碼爲例,只需使用DateAdd()即可將日期遞增1個日曆日;

<% 
inDate = oRS("ordDate") 
'Add one day 
inDate = DateAdd("d", 1, inDate) 

' get each date component 
thisMonth = datepart("M",inDate) 
thisDay = datepart("D",inDate) 
thisYear = datepart("yyyy",inDate) 

' check for the length of the month and day components 
if len(thisMonth) < 2 then thisMonth = "0" & thisMonth 
if len(thisDay) < 2 then thisDay = "0" & thisDay 

' Create the date in your format you posted above 
OrderDate = thisYear & "-" & thisMonth & "-" & thisDay 
%> 

對於日期見Classic ASP - Format current date and time工作的更多信息。

+0

你沒看過他的評論嗎?他已經做到了,並且效率更高! –

+0

@AllBlond他做到了,但這不僅僅是爲了他的利益,你的答案提供了一種解決方法,需要額外驗證生成的日期。這個解決方案更接近OP最終要求和做的。如果OP添加了我建議的答案,我會刪除我的。 – Lankymart

+0

這是我的觀點。你爲什麼給他發佈他的答案?如果他喜歡,他可以做,並接受他的解決方案作爲答案,而不是你。 –