Home » C#

How to add seconds in the current date time in C#?

C# | DateTime.AddSeconds() Method Example: Here, we are going to learn how we can add the seconds in the current date and time in C#?
Submitted by IncludeHelp, on October 18, 2019

To add seconds in the current date-time, we use AddSeconds() method of DateTime class in C#.

Syntax:

    DateTime DateTime.AddSeconds(double);

AddSeconds() method accepts the value of the seconds as double and returns the DateTime object that can be parsed and updated date time can be found.

C# code to add seconds in current date time

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating an object of DateTime class
            //and, initializing it with the current time 
            //using "Now"           
            DateTime dt1 = DateTime.Now;

            //printing current date time
            Console.WriteLine("Currnt date time is: " + dt1.ToString());
            
            //another DateTime object to store the updated date time
            //adding 50 seconds
            DateTime dt2 = dt1.AddSeconds(50);
            //updated date time is 
            Console.WriteLine("Updated date time is: " + dt2.ToString());

            //adding 172800 seconds‬ (i.e. 2 days) in the time 
            dt2 = dt1.AddSeconds(172800);
            //updated date time is 
            Console.WriteLine("Updated date time is: " + dt2.ToString());

            //just to print a new line
            Console.WriteLine();
        }
    }
}

Output

RUN 1:
Currnt date time is: 10/17/2019 5:25:17 PM
Updated date time is: 10/17/2019 5:26:07 PM
Updated date time is: 10/19/2019 5:25:17 PM

RUN 2:
Currnt date time is: 10/17/2019 5:26:05 PM
Updated date time is: 10/17/2019 5:26:55 PM
Updated date time is: 10/19/2019 5:26:05 PM





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.