C Sharp/DateTime/AddMinutes

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators
C# - Flow Control
C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

Contents

[edit] Using the DateTime.AddMinutes Method

To use the DateTime.AddMinutes method you will not specify a timespan. You must specify the amount of minutes you want to add. It accepts a double value so it can calculate partial Minutes. Remember, a timespan must NOT be used with DateTime.AddMinutes.


[edit] Syntax

DateTime.AddMinutes(<double>)

[edit] Example 1:

using System;
class Program
{
    static void Main()
    {
        DateTime today = DateTime.Now;
 
        Console.WriteLine("today: " + today.ToString());
        today = today.AddMinutes(17);
        Console.WriteLine("New Date: " + today.ToString());
        Console.Read();
    }
}

Output:

today: 4/5/2007 8:28:51 PM
New Date: 4/5/2007 8:45:51 PM


As you can see the AddMinutes method simply added 17 minutes to the current date.

[edit] Example 2:

This example will add partial minutes.

using System;
class Program
{
    static void Main()
    {
        DateTime today = DateTime.Now;
 
        Console.WriteLine("today: " + today.ToString());
        today = today.AddMinutes(1.2);
        Console.WriteLine("New Date: " + today.ToString());
        Console.Read();
    }
}

Output:

today: 4/5/2007 8:45:50 PM
New Date: 4/5/2007 8:47:02 PM

As you can see the program added a partial minute and it also added the appropriate minutes.



<-- DateTime Tutorial