CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Posts
    170

    [RESOLVED] DateTime object question

    I want to pick startTime (DataTime object) randomly from 06:00 to 10:00. So far (and very very wrong) I have come up with this. How can I accomplished this with code that actually works?

    Code:
    Random stime = new Random();
    int startTime = stime.Next( 6, 10 );
    
    DateTime dt = new DateTime();
    dt = (00:00) + startTime;

  2. #2
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: DateTime object question

    Hi,

    How about this;

    Code:
    Random stime = new Random();
    int hours = stime.Next( 6, 10 );
    int minutes = stime.Next(0, 60);
    int seconds = stime.Next(0, 60);
    
    int year = 2007;
    int month = 3;
    int day = 1;
    
    DateTime dt = new DateTime(year, month, day, hours, minutes, seconds);
    Regards,

    Laitinen
    Last edited by laitinen; March 1st, 2007 at 10:16 AM.

  3. #3
    Join Date
    May 2006
    Posts
    170

    Exclamation Re: DateTime object question

    Yeah but I want the result in this form:

    06:00

    Don't know how to.

    Oh found something

    dtDob.Format("F",null);

    gonna try this in combination with your code thanks
    Last edited by Visslan; March 1st, 2007 at 10:30 AM.

  4. #4
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: DateTime object question

    Code:
    long tick1 = new DateTime(1,1,1,6,0,0).Ticks / 10000000;
    long tick2 = new DateTime(1,1,1,10,0,0).Ticks  / 10000000;
    Random r = new Random(DateTime.Now.Millisecond);
    long tick3 = r.Next((int)tick1,(int)tick2);
    DateTime d = new DateTime(tick3 * 10000000);
    string s = d.ToShortTimeString();

  5. #5
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

  6. #6
    Join Date
    May 2006
    Posts
    170

    Re: DateTime object question

    will do thank you!

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

Featured