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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With