1

Ok so my code works fine, however I need it to add one day to the OrderDate , while keeping it in the same format yyyy-mm-dd any help is greatly appreciated!

<%
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
%>
user692942
  • 16,398
  • 7
  • 76
  • 175
BWalt302
  • 13
  • 7

2 Answers2

1

Using your code as an example just use DateAdd() to increment the day by 1 calendar day;

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

For more information of working with dates see Classic ASP - Format current date and time.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • @AllBlond He did but it's not just for his benefit, your answer gives a workaround that requires extra validation of the generated date. This solution is closer to what the OP asked and did in the end. If the OP adds an answer like I've suggested I'll remove mine. – user692942 Apr 17 '14 at 14:35
  • That is my point. Why did you post his answer for him? If he like, he can do it and accept his solution as an answer, not you. – All Blond Apr 17 '14 at 14:37
  • @AllBlond Like I said *if* he posts an answer then I'll remove mine. – user692942 Apr 17 '14 at 14:38
  • @AllBlond What are you on about? `inDate = DateAdd("d", 1 inDate)` has taken the original date in `oRS("ordDate")` and incremented it by 1 any "manipulation" after that using `DatePart()` happens on the incremented date so if the original date was say `30-April-2014` the incremented date would become `2014-05-01` (`01-May-2014`)... **Oh ok so you removed your comment**. So this makes no sense out of context, fine. – user692942 Apr 17 '14 at 14:47
0

try this:

 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

this way you preserve original date(thisDay) for manipulation and order date became tomorrow (or whatever) date.

All Blond
  • 802
  • 1
  • 8
  • 16
  • 2
    I came back to delete the question, I was able to just use: thisDay = datepart("D",DateAdd("D", 1, indate)) – BWalt302 Apr 17 '14 at 14:05
  • that will work to, but you may want to keep original date for record keeping. Especially if you dealing with money transactions. – All Blond Apr 17 '14 at 14:07
  • The purpose of this code is just to generate an estimated shipping date, which is most always 1 day after the order, sometimes same day. But the original date is still available in the orders table. Thanks for the help! I went ahead and chose your response as an answer. – BWalt302 Apr 17 '14 at 14:10
  • @BWalt302 Personally I would have prefered you add your own answer as that is more accurate as All Blond's answer will generate a string date not a date datetype, if you're comparing dates etc your approach is better using `DateAdd()`. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer). – user692942 Apr 17 '14 at 14:16
  • @AllBlond Tell me what's to stop someone passing `30-Apr-2014` and ending up with `31-April-2014`? (which doesn't exist as there is only 30 days in April). Use `DateAdd()` as it conforms to the calendar. – user692942 Apr 17 '14 at 14:21
  • @Lankymart: I am pretty sure that question owner smart enough to verify proper date range for any month. I do not doubt ability of the person because he asked question here. – All Blond Apr 17 '14 at 14:24
  • @AllBlond You're missing the point these questions and the answers are for the whole community not just the OP. Plus you're adding an extra layer that isn't required. – user692942 Apr 17 '14 at 14:33