Penguen
Penguen

Reputation: 17298

String to time format but how?

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?

Upvotes: 1

Views: 556

Answers (6)

Justin Wignall
Justin Wignall

Reputation: 3510

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)

Upvotes: 1

lobotomy
lobotomy

Reputation:

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

Upvotes: 0

lobotomy
lobotomy

Reputation:

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

Upvotes: 0

Kjetil Watnedal
Kjetil Watnedal

Reputation: 6167

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.

Upvotes: 0

Spence
Spence

Reputation: 29352

System.TimeSpan s = new TimeSpan();
s.Add(new TimeSpan(days, hours, minutes, seconds, milliseconds))

Upvotes: 2

RvdK
RvdK

Reputation: 19800

You mean:

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

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

Upvotes: 7

Related Questions