Hi,
I want to know how to increment a variable's value which is of type DateTime by half an hour.
i.e if var is a variable of type double we can increment it as
for(double var=9.30;var<=16.30;var+=0.30)
similarly if var is of type time then how can I increment that by half an hour.
plz help
thanks
Vimal KandasamyPosted May 25, 2009, 8:15 AM
sachi vasishtaPosted May 26, 2009, 1:19 AM
sachi vasishtaPosted May 25, 2009, 7:46 AM
thanks vimal.
Vimal if I want to display the time in 24 format which function shall I use because I have tried with functions like ToShortTimeString, etc but the DateTime.Now; displays time in AM/PM format but I want to have timings in 24 hours format. How to get this
thanks
Mohit JainPosted May 25, 2009, 5:59 AM
Hi Sachi,
You want to increase the time only one time because the example you have given of double, it is a loop which increase the value at every loop. I believe you want to increase the time by half hour only one ime. DateTime class provide a method AddHour which can be used to increase the time by half hour. As in the examble below I am increasing the Current time value by Half an hour.
Response.Write(
DateTime.Now.AddHours(.5).ToString());Hope this help
Vimal KandasamyPosted May 25, 2009, 3:29 AM
for your code.it will be
sachi vasishtaPosted May 25, 2009, 3:14 AM
thanks suchit for your kind reply
suchit but the addhours function is not working. I have written the following code
private
void Form6_Load(object sender, EventArgs e){
DateTime time = DateTime.Now;time.AddHours(0.5);
textBox1.Text = time.ToString();
//DateTime time1 = time; //time.AddHours(0.5); //textBox2.Text = time.ToString();}
Here the textbox is displaying the current time. The thing is I want to display the time like 09:30:00 Am in first row of a datagridview followed by 10:00:00 Am in the next row and so on( I want to add half an hour for the previous time)
thanks
Suchit KhannaPosted May 25, 2009, 2:13 AM
Please try to use intellisense and you could find many functions there for such tasks... anyways here is a sample code which could help you:
class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
Console.WriteLine("Current time is : {0}", time);
Console.WriteLine("Time after half hour will be:{0}", time.AddHours(0.5));
Console.Read();
}
}