?
Solved

Easy datetime question

Posted on 2004-10-13
2
Medium Priority
?
192 Views
Last Modified: 2010-04-23
I have a DateTime varible with a date and a time in it.  My question is how do I change the date of that variable but not the time.
0
Comment
Question by:mrbass78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
bramsquad earned 252 total points
ID: 12301038
DateAdd(DateInterval.Day, 4.0, Now)

where the '4.0' value is the amount of days you wish to add/subtract

and 'Now' is the date value you wish to change

~b
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 248 total points
ID: 12301160
Here are some more examples:

        Dim myDate As DateTime = DateTime.Now

        MsgBox(myDate, MsgBoxStyle.Information, "Initial DateTime")

        ' change the date using relative adjustments
        myDate = myDate.AddDays(1) ' add one day
        myDate = myDate.AddMonths(1) ' add one month
        myDate = myDate.AddYears(1) ' add one year
        MsgBox(myDate, MsgBoxStyle.Information, "After Relative Changes to DateTime")

        ' change the date using an absolute date while preserving the time
        myDate = DateTime.Parse("10/29/1973 " & myDate.ToLongTimeString)
        MsgBox(myDate, MsgBoxStyle.Information, "After Absolute Change to DateTime")
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question