How to print tomorrow’s datetime in C#:
In this post, we will learn how to print the DateTime of tomorrow in C#. We will learn two different ways to print that in this post.
By using DateTime:
This is the easiest way to do that. DateTime struct provides the current day as Today. We can add days to this by using AddDays method.
If we pass 1 to AddDays, it will add one to today and give us one DateTime for tomorrow.
C# program:
Below is the complete C# program:
using System;
public class Program
{
public static void Main()
{
var today = DateTime.Today;
var tomorrow = today.AddDays(1);
Console.WriteLine("Tomorrow: "+tomorrow.ToString());
}
}
It will print tomorrow’s date:
Tomorrow: 8/31/2021 12:00:00 AM
How to print the day of tomorrow:
We can use the same program we used above. Once we get a DateTime variable, we can print the day by using the DayOfWeek parameter.
using System;
public class Program
{
public static void Main()
{
var today = DateTime.Today;
var tomorrow = today.AddDays(1);
Console.WriteLine("Tomorrow: "+tomorrow.DayOfWeek.ToString());
}
}
It will print tomorrow’s day:
Tomorrow: Tuesday
How to use a different format to the datetime:
We can pass a formatter string to the ToString method and it will format the DateTime based on that.
using System;
public class Program
{
public static void Main()
{
var today = DateTime.Today;
var tomorrow = today.AddDays(1);
Console.WriteLine("Tomorrow: "+tomorrow.ToString("dd:MM:yyyy"));
}
}
It will print one output as like below:
Tomorrow: 31:08:2021
Print tomorrow’s date by using TimeSpan:
TimeSpan object can be created for one day and we can add this to today to get tomorrow’s DateTime.
using System;
public class Program
{
public static void Main()
{
var today = DateTime.Today;
var oneDay = new TimeSpan(1, 0, 0, 0);
var yesterDay = today + oneDay;
Console.WriteLine("Tomorrow "+yesterDay.ToString());
}
}
It will print:
Tomorrow 8/31/2021 12:00:00 AM
You might also like:
- C# program to find the area and perimeter of a rectangle
- C# program to find the index of an element in an array
- C# program to remove duplicate characters from a string
- C# program to convert a string to byte array
- C# program to find the area of a triangle
- C# program to get the OS version using Environment class