2013-04-15 22 views
0

我有一個日曆程序,駐留在服務器上,該程序存儲日期,格式數據庫:如何應對數字6個位數的日期

YYYYMMDD

有在asp中管理這些日期的「簡單」方法?例如,假設我們有2013年4月20日爲一個日期:

20130420 

我們要到20天加入到這個日期,所以我們想製作:

20130510 

什麼想法?在asp中有沒有integerTOdate轉換?或者我可以用來給號碼添加「天數」20130420

+1

從20130420起的20天是20130510,而不是20130509! :) – Dave

+0

我真的沒有得到-1 –

回答

3

像這樣(未經):

Dim strDate, theDate, theDatePlusTwentyDays 
Dim Year, Month, Day 

' The date is stored as a string... 
strDate = "20130420" 

' Extract the components of the date 
Year = Mid(strDate, 1, 4) 
Month = Mid(strDate, 5, 2) 
Day = Mid(strDate, 7, 2) 

' Convert the components of the date into a datetime object 
theDate = DateSerial(Year, Month, Day) 

' Add 20 days using DateAdd 
theDatePlusTwentyDays = DateAdd("d", 20, theDate) 
+1

Allthough錯誤的年份= ...','暗月= ...'和Dim Day = ...'(你不能在VBScript中一次性聲明和分配,也許你想糾正它),我喜歡這個比Dave Rooks解決方案更好,因爲日期以字符串形式存儲(不要以整數存儲其他任何內容)和使用'DateSerial'。 – AutomatedChaos

+0

@AutomatedChaos - 乾杯!將更新。自從在VBScript中做任何事以來已經有一段時間了。 –

2

是的,你可以使用DateAdd功能

Response.Write(DateAdd("d",1,Now())) 

您需要首先進行格式化你的日期,雖然成類似

<% 
dim _dateOnServer 
dim _formattedDate 
dim _day 
dim _month 
dim _year 

_dateOnServer = 20130420 

_year = Left(_dateOnServer,4) 
_month = Mid(_dateOnServer,5,2) 
_day = Right(_dateOnServer,2) 


_formattedDate = _month &"-"& _day &"-"& _year 

dim _newDate 
_newDate = DateAdd("d",20, _formattedDate) 

_day = Left(_newDate,2) 
_month = Mid(_newDate,4,2) 
_year = Right(_newDate,4) 

dim _newDateFormat 
_newDateFormat = _year & _month & _day 

%> 
+0

我想這是正確的方式...我試試看... –

1

一個解決方案,可以完全控制訂單和格式...

strDay = Day(Date) 
strMonth = Month(Date) 
strYear = Year(Date) 
strHours = Hour(Now) 
strMins = Minute(Now) 
strSecs = Second(Now()) 
    if len(strMonth) = 1 then 
     strMonth = "0" & strMonth 
    end if 
    if len(strDay) = 1 then 
     strDay = "0" & strDay 
    end if 
    if len(strHours) = 1 then 
     strHours = "0" & strHours 
    end if 
    if len(strMins) = 1 then 
     strMins = "0" & strMins 
    end if 
    if len(strSecs) = 1 then 
     strSecs = "0" & strSecs 
    end if 

strDateAdded = strYear & "-" & strMonth & "-" & strDay 
strDateAddedTime = strDateAdded & " " & strHours & ":" & strMins 

使用此方法,您可以完全控制訂單,即使在不同時區運行您的網絡應用程序,您仍然保持DD/MM格式...或任何您想要的順序,如MM-DD- YY(通過重新排序和修整年份)。我個人更喜歡YYYY-MM-DD因爲ASC和DESC排序是一個容易得多的工作,即:更容易閱讀,因爲所有行將具有相同數目的相同的字符:

2013-04-01 03:15 
2013-04-09 10:15 
2013-04-22 07:15 
2013-04-23 10:15 
2013-04-23 10:60 
2013-10-25 12:01 
2013-10-25 12:59 

相反的:

2013-4-1 3:15 
2013-4-9 10:15 
2013-4-22 7:15 
2013-4-23 10:15 
2013-4-23 10:60 
2013-10-25 12:1 
2013-1-25 12:59 
相關問題