0

我有一个日历程序,它驻留在服务器上,该程序将日期以下列格式存储到数据库中:

YYYYMMDD

有没有一种“简单”的方法可以在asp中管理这些日期?例如,假设我们将 2013 年 4 月 20 日作为日期:

20130420

我们想在这个日期上增加 20 天,所以我们想产生:

20130510

任何想法?在 asp 中是否有 integerTOdate 转换?或者我可以用来在数字上添加“天”的东西20130420

4

3 回答 3

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)
于 2013-04-15T14:40:04.373 回答
2

Yes, you can use the DateAdd function

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

You need to format your date first though into something like

<%
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

%>
于 2013-04-15T14:29:41.383 回答
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

使用这种方法,您可以完全控制顺序,即使在不同时区运行 Web 应用程序时,您仍然可以保持 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
于 2013-04-29T08:40:06.103 回答