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
Solved

Easy datetime question

Posted on 2004-10-13
2
188 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
2 Comments
 
LVL 8

Accepted Solution

by:
bramsquad earned 63 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 62 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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