兔八哥极品软件园    运行: 4514天 | 文章:640 篇 | 评论:575 条 | 碎语:1条

C# 和SQL Server中DateTime的用法

作者:admin 发布于:2014-7-3 10:04 Thursday 分类:网络转载


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

  1. Today: This displays today's date. The time value is 12:00:00.

  2. 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

 

DateTime-in-Csharp.jpg

SQL Server Getdate Function

The GETDATE() function returns the current date and time from the SQL Server.

SELECT GETDATE() AS [DateTime]

Output

SQLDateTime1.jpg

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

Current-Date-without-time-in-Csharp.jpg

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

SQLDateTime2.jpg

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

 

Tomorrow-Date-without-time-in-Csharp.jpg

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

SQLDateTime3.jpg

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

 

Start-Date-of-Last-Month-in-Csharp.jpg

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

SQLDateTime4.jpg

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

 

Endt-Date-of-Last-Month-in-Csharp.jpg

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

SQLDateTime5.jpg

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

 

Start-Date-of-Current-Month-in-Csharp.jpg

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

SQLDateTime6.jpg


Powered by 兔八哥极品软件 苏ICP备12049267号 sitemap