-
March 1st, 2007, 10:00 AM
#1
[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;
-
March 1st, 2007, 10:13 AM
#2
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.
-
March 1st, 2007, 10:26 AM
#3
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.
-
March 1st, 2007, 10:28 AM
#4
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();
-
March 1st, 2007, 10:32 AM
#5
Re: DateTime object question
Just add this;
Code:
string timeString = dt.ToShortTimeString();
Regards,
Laitinen
-
March 1st, 2007, 10:46 AM
#6
Re: DateTime object question
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|