Closed Thread
Results 1 to 2 of 2

Thread: Adding Hour to datetime...

  1. #1
    nick3 is offline Newbie
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Adding Hour to datetime...

    Hello, Im using a MonthCalender to get a dateTime, and just a textbox to let the user enter hours and minutes, so now I just want to add this so say the user enters 10:20 then I would like it to be (for example) 1999-10-10 10:20:00 instead of 1999-10-10 00:00:00, so I tryed to convert the string 10 to a double and the string 20 to an other double and trying to use dateTime.addhour(hours); But this wont work, the datetime is still 1999-10-10 00:00:00... I dont have my code on this computer, if you dont get what I mean i can post it, but we can start with this

    Here is the code now too!!

    Code:
                DateTime dtStart, dtEnd;
                TimeSpan startHour, startMin, endHour, endMin;
                dtStart = this.mcStartDate.SelectionStart;
                dtEnd = this.mcEndDate.SelectionStart;
                startHour = TimeSpan.FromHours(Convert.ToDouble(this.mtbStartTime.Text.Substring(0,2)));
                dtStart = dtStart.Add(startHour);
    startHour is 10:00:00 if you enter 10 hours, but i still get 1999-10-10 00:00:00

    /Nick
    Last edited by nick3; 01-19-2010 at 08:06 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: Adding Hour to datetime...

    DateTime object is not mutable. When you use AddHours() method it returns new DateTime object.

    Code:
    using System;
    
    namespace ConsoleApplication1
    {
    	class Program
    	{
    		static void Main()
    		{
    			var date = DateTime.Today;
    			date = date.AddHours(10);
    			Console.WriteLine(date);
    
    			Console.ReadLine();
    		}
    	}
    }

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. DateTime Picker
    By DeltaCmdr in forum C# Programming
    Replies: 3
    Last Post: 06-27-2011, 09:20 AM
  2. Algorithm to convert 24 hour format into 12 hour
    By jclarke in forum C and C++
    Replies: 12
    Last Post: 03-14-2011, 11:22 AM
  3. Help using SQL and datetime
    By Orush7 in forum C# Programming
    Replies: 3
    Last Post: 01-15-2011, 08:02 AM
  4. datetime data type
    By giuliel in forum Database & Database Programming
    Replies: 3
    Last Post: 08-20-2008, 01:04 PM
  5. datetime
    By moonrise in forum C# Programming
    Replies: 1
    Last Post: 05-15-2006, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

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