Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting date only

Tags:

vb.net

I want to get today's date. Using Now(), I get both date and time but I need date only. How to get it? Thanks Furqan

like image 853
Furqan Sehgal Avatar asked Feb 01 '11 09:02

Furqan Sehgal


People also ask

How can I get only date from DateTime in SQL?

CONVERT or CAST function In SQL Server 2008 and above, we can either use the CONVERT or CAST function to return the DATE part from the DATETIME datatype.

How can I get current date data in SQL?

SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.

How can I get current date without time in SQL?

However, if you want to get just the current date – not the date and the time – you can use the CAST() function. This function takes any expression or any column name as the first argument. Then you use the keyword AS and enter the new data type to return.


2 Answers

Use System.DateTime.Today. Some additional notes:

  • While you're only interested in the date, you still get a System.DateTime object. That is, there still is a time portion in the returned value, but it's left blank (all zeroes).

    It's important to remember this when you compare dates. If you want to compare two dates, and you've fetched one date via DateTime.Today and another date via DateTime.Now, then these two dates will not be equal — precisely because one has the time set and the other doesn't.

  • In VB.NET, the Date keyword is equivalent to the System.DateTime type. So you can write:

    Date.Today
    

    P.S.: It just occurred to me that it might be a good idea to write Date.Today, but DateTime.Now, in order to make it even more explicit that the former property only returns the date portion, while the latter returns both a date and time portion.

like image 118
stakx - no longer contributing Avatar answered Oct 05 '22 15:10

stakx - no longer contributing


Try the DateTime.Today method for just the current date.

This returns a DateTime object, just like DateTime.Now, but it's time portion is always set to 0.

like image 37
SWeko Avatar answered Oct 05 '22 15:10

SWeko