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.
The Clock. systemUTC(). instant() method returns the current date and time both.
Well, you can get just today's date as a DateTime
using the Today
property:
DateTime today = DateTime.Today;
or more generally, you can use the Date
property. For example, if you wanted the UTC date you could use:
DateTime dateTime = DateTime.UtcNow.Date;
It's not very clear whether that's what you need or not though... if you're just looking to print the date, you can use:
Console.WriteLine(dateTime.ToString("d"));
or use an explicit format:
Console.WriteLine(dateTime.ToString("dd/MM/yyyy"));
See more about standard and custom date/time format strings. Depending on your situation you may also want to specify the culture.
If you want a more expressive date/time API which allows you to talk about dates separately from times, you might want to look at the Noda Time project which I started. It's not ready for production just yet, but we'd love to hear what you'd like to do with it...
See, here you can get only date by passing a format string. You can get a different date format as per your requirement as given below for current date:
DateTime.Now.ToString("M/d/yyyy");
Result : "9/1/2015"
DateTime.Now.ToString("M-d-yyyy");
Result : "9-1-2015"
DateTime.Now.ToString("yyyy-MM-dd");
Result : "2015-09-01"
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
Result : "2015-09-01 09:20:10"
For more details take a look at MSDN reference for Custom Date and Time Format Strings
Use DateTime.Today
property. It will return date component of DateTime.Now
. It is equivalent of DateTime.Now.Date
.
Current Time :
DateTime.Now.ToString("HH:mm:ss");
Current Date :
DateTime.Today.ToString("dd-MM-yyyy");
You can use following code to get the date and time separately.
DateTime now = DateTime.Now;
string date = now.GetDateTimeFormats('d')[0];
string time = now.GetDateTimeFormats('t')[0];
You can also, check the MSDN for more information.
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