views:

282

answers:

6

I want to change a value from int or string format to datetime format. There is any function in SQL like the following?:

    Function:                        Result            

    TimeAdd( nextrundate,"sec",45)   00:00:45
    TimeAdd( nextrundate,"min",45)   00:45:00
    TimeAdd( nextrundate,"hour",4)   04:00:00

But:

    TimeAdd( nextrundate,"min",70)   01:10:00
    TimeAdd( nextrundate,"min",190)  03:10:00

Is there a method that does this in C# also?

+7  A: 

You mean:

  • TimeSpan.FromSeconds(double)
  • TimeSpan.FromMinutes(double)

see MSDN: http://msdn.microsoft.com/en-us/library/system.timespan_members(VS.80).aspx

PoweRoy
+2  A: 
System.TimeSpan s = new TimeSpan();
s.Add(new TimeSpan(days, hours, minutes, seconds, milliseconds))
Spence
A: 

In C# you can use the DateTime and TimeSpan classes.

DateTime rundate = DateTime.Now();
DateTime nextRunDate= rundate .AddDays(1);
TimeSpan oneDay=(TimeSpan)(nextRunDate-rundate);

There are similar methods for minutes, seconds etc.

Kjetil Watnedal
A: 

DateTime d = new DateTime(); d = d.AddMinutes(70); Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));

A: 
DateTime d = new DateTime();
d = d.AddMinutes(70);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"));
+1  A: 

In SQL you could use something like

convert(varchar(8),dateadd(second,45,nextrundate),114) convert(varchar(8),dateadd(minute,45,nextrundate),114) convert(varchar(8),dateadd(hour,4,nextrundate),114)

Justin Wignall