Setting the day on a date

I am allow the use to specify the day of the month, is there an easy way of changing the date to reflect the new day.

So say the date is set to 13/12/2001 and the user sets the day to 15 the date would then show 15/12/2011 without breaking the date up then concatenating it back together gain,


rocky050371Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Arthur_WoodCommented:
where are you using/setting the date?

this is one way:

Dim testDate as Date
testDate = DateSerial(2001, 12, 15)

this ia another way.  Assume Today is Dec 13, 2001

Dim testDate as Date
testDate = DateAdd("d", Today(), 2)

AW
0
rocky050371Author Commented:
"So say the date is set to 13/12/2001 and the user sets the day to 15 the date would then show 15/12/2011 without breaking the date up then concatenating it back together gain,"

Effectively the month and year will remain the same to whatever is previously set, the day will change.

0
Arthur_WoodCommented:
if you have a variable which alreadys holds the data as "13/12/2011", then you can use the DateAdd function to add to that variable:

assume the data variable is named testDate, and it currently is set to the data 13/12/2011.

testDate = DateAdd("d", 2, testDate)

I was incorrect in my first post.  The paramaters for DateAdd are (interval, number, date)

see the explanation of the function at

http://www.vb6.us/tutorials/understanding-vb6s-dateadd-function

AW
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

rocky050371Author Commented:
Sorry, your still missing the problem, the day can be set to anything 1st, 30th etc
0
keyuCommented:
you are saying its "13/12/2001" means the year "2001"  and if someone enter 15 it should be "15/12/2011" so now the year will be "2011"  

if that is the case

orgdate= CDate("13/12/2001")
mydate = CDate(testday &"/"&month(date)&"/"year(date))

// here "testday" is date value enter by user

MsgBox DateDiff("d", orgdate,mydate)
0
rocky050371Author Commented:
The year is a typo, the question was apart from concatenating.
0
keyuCommented:
Not sure what you exactly want but try this..

testday =Request.form("testday")
orgdate= CDate("13/12/2001")
myday=day(orgdate)

daydf=myday-testday

mydate = DateAdd("d",daydf, orgdate)

If Not IsDate(mydate) Then
    MsgBox "please type a valid day for date!"
Else
    MsgBox "is a date " & DateValue(mydate)
End If



0
rocky050371Author Commented:
That will not work, what is they enter 1. I will also surround the the resulting date with a try parse.


0
Arthur_WoodCommented:
can you explain in some more detail the precise nature of the problem.  What are you trying to allow the user to do?

Is this in VB 6 or VB.net.

Is theis for a desktop application or a web page?

I do not understand what you are saying here:
"That will not work, what is they enter 1. I will also surround the the resulting date with a try parse. "

AW


0
CluskittCommented:
Dim datOrig As Date
Dim intDay As Integer
'Assign a value to datOrig and to intDay
datOrig = Dateadd(DateInterval.Day, datOrig.Day - intDay, datOrig)
0
rocky050371Author Commented:
The zone is .net, the type of application is irrelevant.

I am not trying to be difficult here, but the question is fairly straightforward, it looks like I will have to break the date to create a new one. I thought there might be a faster way of simply replacing the day portion of a date with another day.
0
keyuCommented:
in above example you are not braking date just finding difference and add that difference in given date..

dont you want that ?
0
rocky050371Author Commented:
the example states

So say the date is set to 13/12/2001 and the user sets the day to 15 the date would then show 15/12/2011

they could quite easily enter into the day box 1 or 31.

without breaking the date up then concatenating it back together gain,

0
Arthur_WoodCommented:
If you just want to change the Day part of the Date - without affecting the Month or the Year, and you want the user to be able to specify haw many days to Add (or Subtract) from the Day part, then use the DateAdd function with the Interval argument set to "d", (or DateInterval.Day in VB.Net - DateInterval.Day is a VB.NET built-in enumeration which is equal to "d"), pass the Original Date and the number of days by which you want to change the day part of the date.

If the user passes a POSITIVE number, the day part of the date will INCREASE by that number of days. SO if the user enters the value 2, and the Original Date was 13/12/2011, the new date would be 15/12/2011.

If the user passes a NEGATIVE number, the day part of the date will DECREASE by that number of days. SO if the user enters the value -2, and the Original Date was 13/12/2011, the new date would be 11/12/2011.

AW
0
rocky050371Author Commented:
Arthur please read the comments above, I don't want the user to add or subtract, I simply want to allow him to specify a day.
0
keyuCommented:
this is what i habve done with my last example

if date is 1 it will return difference in -12 and substract 12 from 13

0
CluskittCommented:
I've already provided you with a solution for that. Just add the difference between the day of the date and the new day desired.
0
Arthur_WoodCommented:
"So say the date is set to 13/12/2001 and the user sets the day to 15 the date would then show 15/12/2011

they could quite easily enter into the day box 1 or 31."

If that is the case, the  you can use the DateSerial function

newDate = DateSerial (Year(oldDate), Month(oldDate), newDate)

this does NOT "break the date up, and the concatenate it back together again".

AW
0
Arthur_WoodCommented:
correction,  should have read

newDate = DateSerial (Year(oldDate), Month(oldDate), newDay)

where the user can specify the value of newDay

AW

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rocky050371Author Commented:
Keyu, sorry I misread your response, That should do the job thanks, I will not be using DateAdd though, I will use AddDays as this is a .net equivalent
0
keyuCommented:
if so Please accept my answer  and assign gread
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.