In this article, I would like to show the similarity of the SQL GetDate function and the C# DateTime class. Many developers use the DataTime function to find information related to date and time in SQL Server. Here we will see how you can do it in C#. We use DateTime to work with dates, times, and both. The DateTime type in the C# language provides useful methods and properties for computing these values. So let's have a look at a practical example of how to use the SQL getdate function and C# DateTime with various queries to find the date and time.
DateTime in C#
This represents an instant in time, typically expressed as a date and time of day. The DateTime class has two main properties to find the current Date.
Today and Now property
-
Today: This displays today's date. The time value is 12:00:00.
-
Now: This displays the current date and time of the system, expressed as the local time. In other words the Now property returns a DateTime object that has the date and time values for right now.
Console.WriteLine("Today: {0}", DateTime.Today);
Console.WriteLine("Today: {0}", DateTime.Now);
Output
SQL Server Getdate Function
The GETDATE() function returns the current date and time from the SQL Server.
SELECT GETDATE() AS [DateTime]
Output
Current Date Without Time in C#
You can do it with the ToString function in C#. The following is simple code for it.
string TodayDatewithouttime = DateTime.Now.ToString("dd/MM/yyy");
Console.WriteLine("Today Date without Time: {0}", TodayDatewithouttime);
Output
SQL Current Date Without Time
The query below returns the date without time.
Declare @date datetime
set @date=DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
select @date
Output
Tomorrow Date Without Time in C#
You can do it with adddays function in C#. The following is simple code for it.
Console.WriteLine("Today: {0}", DateTime.Now);
string TomorrowDatewithouttime = DateTime.Now.AddDays(1).ToString("MM/dd/yyy");
Console.WriteLine("Tomorrow Date without Time: {0}", TomorrowDatewithouttime);
Output
SQL Tomorrow Date Without Time
The query below returns Tomorrow's date without time.
Declare @date date
set @date=DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
select @date
Output
Start Date of Last Month in C#
You can do it in C#. The following is simple code for it.
Console.WriteLine("Today: {0}", DateTime.Now);
var today = DateTime.Now;
var month = new DateTime(today.Year, today.Month, 1);
var first = month.AddMonths(-1);
Console.WriteLine(" Start Date of Last Month : {0}", first.ToString("yyy/MM/dd"));
Output
SQL Start Date of Last Month
The query below returns the start date of the previous month.
DECLARE @StartDateofLastMonth DATETIME
SET @StartDateofLastMonth = DATEADD(mm, DATEDIFF(mm, 0, getdate()) - 1, 0)
select @StartDateofLastMonth
Output
End Date of Last Month in C#
You can do it in C#. The following is simple code for it.
Console.WriteLine("Today: {0}", DateTime.Now);
var today = DateTime.Now;
var month = new DateTime(today.Year, today.Month, 1);
var last = month.AddDays(-1);
Console.WriteLine(" End Date of Last Month : {0}", last.ToString("yyy/MM/dd"));
Output
SQL End Date of Last Month
The query below returns the end date of the previous month.
DECLARE @StartDateofLastMonth DATETIME, @EndDateofLastMonth DATETIME
SET @StartDateofLastMonth = DATEADD(mm, DATEDIFF(mm, 0, getdate()) - 1, 0)
SET @EndDateofLastMonth = dateadd(dd, -1, DATEADD(mm, 1, @StartDateofLastMonth))
select @EndDateofLastMonth
Start Date of current Month in C#
You can do it in C#. The following is simple code for it.
Console.WriteLine("Today: {0}", DateTime.Now);
var today = DateTime.Now;
var StartDate = new DateTime(today.Year, today.Month, 1);
Console.WriteLine("Start Date of current Month : {0}", StartDate.ToString("yyy/MM/dd"));
Output
SQL Start Date of Current Month
The query below returns the start date of the current month.
DECLARE @StartDateofLastMonth DATETIME
SET @StartDateofLastMonth = DATEADD(month, datediff(month, 0, getdate()), 0)
Select @StartDateofLastMonth
Output