0

got a problem. I want to convert two doubles to a time format (HH:mm)(2.5 equals to 02:30 for example). I got a hour int variable and minute int variable. The problem is that I don't know what to do with them, I mean how I import them to the date time picker. At the beginning the time is stored in a string in an array.

public {
    int hour = 5;
    int min =40;
    time.Value = new DateTime(hour, min);// ***this is a mistake***
}

The outcome should be 05:40 in a datepicker

misha312
  • 1,443
  • 4
  • 18
  • 27
  • check this http://stackoverflow.com/questions/6862684/converting-from-decimal-degrees-to-degrees-minutes-seconds-tenths – Shoaib Shaikh Apr 21 '13 at 20:14

3 Answers3

3

not sure about your requirement, but something like below,

time.Value = DateTime.Today.AddHours(hours).AddMinutes(minutes);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • I don't want to add the hours and the minutes to the datepicker. I want it to show only the time in the variables. Thanks a lot by the way... – misha312 Apr 20 '13 at 15:00
  • @misha312 what is the difference between your answer and my answer? i'm getting current date and set hours and minutes. finally it will give you the value you want – Damith Apr 21 '13 at 19:31
  • 1
    `DateTime.Now.Date` can be replaced by the simpler `DateTime.Today`. –  Apr 21 '13 at 19:33
1

Figured out the answer.

DetaTimePicker.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
                   DateTime.Now.Day, hour integer, minute integer, 0);

The zero is the minutes.

Thanks guys.

Damith
  • 62,401
  • 13
  • 102
  • 153
misha312
  • 1,443
  • 4
  • 18
  • 27
0

SET time as 13:30

 DateTimePicker newDate = (DateTimePicker)rootcontrol;
 String format = "H:mm";
 DateTime dDate;
     try
      {
        DateTime.TryParseExact(answer, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dDate);
       newDate.Format = DateTimePickerFormat.Custom;
        newDate.Value = dDate;
      }
       catch (System.ArgumentOutOfRangeException)
     {
        newDate.Text = answer;
        Console.WriteLine("ERROR SET TIME:" + answer);
      }