subtracting from the current date

Posted on 2004-11-04
Last Modified: 2010-04-15
I have a date value and I need to subract it from the current date(DateTime.Now). Which is a timespan value so therefore this calculation is not working.

MyTime = a datetime value from my database.

I need to subract:

NewTime = DateTime.Now - MyTime;

I cannot seem to figure out how to get this to work!

any help would be great!

thanks Jordan

Question by:jrmcdona
    LVL 8

    Expert Comment

    DateTime date2 = DateTime.Now;
    DateTime date1 = MyTime;
    TimeSpan diff1 = date2.Subtract(date1);
    LVL 8

    Accepted Solution

    Of course this should work too...

    TimeSpan diff2 = date2 - date1;
    LVL 19

    Assisted Solution

    DateTime MyTime = DateTime.Parse("12/12/2003"); // this will be from database
    DateTime NewTime = DateTime.Now.Add( -(DateTime.Now- MyTime));

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    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…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now