Results 1 to 10 of 10

Thread: [RESOLVED]How can I multiply a date

  1. #1

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Resolved [RESOLVED]How can I multiply a date

    Hello,
    It's me again...How can I multiply a date like for example I have 14/12/2004 on a dd-mm-yyyy format. On my code I want it to multiply it by term and every term composed 1 month. and I want to display it again into the same form dd-mm-yyyy format.

    Ex. 14/12/2004 * 12 = 14/12/2005 ' I want to get this


    Thanks a lot of you guys out there
    Last edited by shyguyjeff; Nov 27th, 2007 at 10:11 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I multiply a date

    Firstly, you don't have a date in any format because Date objects have no format. Format is only an issue when displaying a string representation of a date, which you certainly aren't doing when performing mathematical calculations on that Date.

    Secondly, multiplying a date makes no sense whatsoever. You might perform addition or subtraction on a date, but certainly not multiplication or division. In your case you're performing addition, i.e. adding 12 months:
    vb.net Code:
    1. myDate = myDate.AddMonths(12)
    Like I said, that date object has no format. If you want to display it to the user you would convert it to a string and display that. It is THEN AND ONLY THEN that format becomes an issue:
    vb.net Code:
    1. myLabel.Text = myDate.ToString("dd-MM-yyyy")
    That said, I strongly recommend that you do not force your preferred date format on the user. Windows allows each user to specify exactly how they want to have dates displayed and you should respect the user's choices wherever possible. To use the system date format selected by the user in the OS options you'd do this:
    vb.net Code:
    1. myLabel.Text = myDate.ToShortDateString()
    Now a US user won't get confused because you've forced the day to be shown first when they expect to see the month first. Everyone will see exactly what they expect to see, as is appropriate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: How can I multiply a date

    Quote Originally Posted by jmcilhinney
    That said, I strongly recommend that you do not force your preferred date format on the user. Windows allows each user to specify exactly how they want to have dates displayed and you should respect the user's choices wherever possible. To use the system date format selected by the user in the OS options you'd do this:
    My preference is to always display dates in the format "dd MMM yyyy" which overcomes any BA ambiguity.

  4. #4

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: How can I multiply a date

    tnx guys out there....it helps ........especially to jmcilhinney...

  5. #5

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: How can I multiply a date

    guys need follow up on my problem....The code works smoothly but how about if I add months that has already date from my database like this 7/10/2006 12:00:00 AM then i want to add moths...is it possible? How can I do that?

    Regards,
    Jeff

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: How can I multiply a date

    Its the same as JCM showed you. Are you casting you dates from your databases into strings?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I multiply a date

    A Date is a Date. If you get a date value from a database then it's stored in a Date. You already know how to add a number of months to a Date. I assume that you already know how to save updates to a database too but, if you don't, follow the Data Access Examples link in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: How can I multiply a date

    Quote Originally Posted by wild_bill
    Its the same as JCM showed you. Are you casting you dates from your databases into strings?
    I am casting from my database and put it in a Textbox....But when I add a months on it. I have 3 textboxes..txtBoxTerm, txtBoxStartDate, txtBoxEndDate..When I add the txtBoxTerm on my txBoxStartDate it result in into txtBoxEndDate...This is my problem to get the txtBoxEndDate...Tnx bill..


    @jm

    tnx jm...ur work is nice...it helps me but sorry if sometime i understand it so hard sometimes..I am just a newbie in programming thats why i keep on asking...Thanks again...God Bless...

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I multiply a date

    You shouldn't be adding anything to the value in the TextBox. That's a String, not a Date. What you should be doing is storing the value from the database in a Date variable. You can, at any point, display that value in a TextBox. If you want to add a number of months then you add them to the Date value, then display that new value in the TextBox again.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member shyguyjeff's Avatar
    Join Date
    Jul 2007
    Location
    City of Durian
    Posts
    289

    Re: How can I multiply a date

    I made it!!!!!!!!!!!!Yepeeeeeeeeehhhhh!!!!!!!!!Thanks jm....your a great wizard!!!!!!!!!Hurrahhh!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width