180

I am able to get date and time using:

DateTime now = DateTime.Now;

How can I get the current date and time separately in the DateTime format itself?

I am not using the DateTime picker dialog box in ASP.NET (C#).

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
bharathi
  • 6,019
  • 23
  • 90
  • 152

15 Answers15

287

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

llrs
  • 3,308
  • 35
  • 68
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • -> Hi. Will UtcNow.Date work for all countries. I want the current date of the current locality. I know to convert full datetime to current timezone. But what if I truncate the time part. I am from India and my friend in california. For me it is 9:30 AM Sunday IST now. For my friend, it is 09:00 PM saturday in Los Angeles now. – Thameem Sep 18 '22 at 04:00
  • @Thameem: Well it will give the *UTC* date regardless, which is why I recommended it for that scenario. The UTC date doesn't depend on where you are, because it uses UTC instead of the local time zone. – Jon Skeet Sep 18 '22 at 14:58
  • So, in order to get the local date. I have to get utc.now, convert it to local time and truncate time part or just read the date. Understood. Thanks – Thameem Sep 18 '22 at 18:11
  • 1
    @Thameem: Yes, or you can just use `DateTime.Today` *if* the system time zone is the one you're interested in. – Jon Skeet Sep 19 '22 at 07:15
47

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

Chiragkumar Thakar
  • 3,616
  • 5
  • 37
  • 49
38

Use DateTime.Today property. It will return date component of DateTime.Now. It is equivalent of DateTime.Now.Date.
Starting from .NET 6 you might also consider using DateOnly and TimeOnly

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
29

Current Time :

DateTime.Now.ToString("HH:mm:ss");

Current Date :

DateTime.Today.ToString("dd-MM-yyyy");
Moory Pc
  • 860
  • 15
  • 16
10

There is no built-in date-only type in .NET.

The convention is to use a DateTime with the time portion set to midnight.

The static DateTime.Today property will give you today's date.

LukeH
  • 263,068
  • 57
  • 365
  • 409
10

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.

Hossein Mobasher
  • 4,382
  • 5
  • 46
  • 73
4

As of .Net 6 you can use:

 var dateOnly = DateOnly.FromDateTime(DateTime.Now);

Ref: https://learn.microsoft.com/en-us/dotnet/api/system.dateonly?view=net-6.0

tibba69
  • 83
  • 4
2

In .Net6, you can use the new DateOnly Type :

DateOnly.FromDateTime(DateTime.Today)
ihebiheb
  • 3,673
  • 3
  • 46
  • 55
1

Use

txtdate.Text = DateTime.Today.ToString("dd-MM-yyyy");
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

I think you need separately date parts like (day, Month, Year)

DateTime today = DateTime.Today;

Will not work for your case. You can get date separately so you don't need variable today to be as a DateTimeType, so lets just give today variable int Type because the day is only int. So today is 10 March 2020 then the result of

int today = DateTime.Today.Day;

int month = DateTime.Today.Month;

int year = DateTime.Today.Year;

MessageBox.Show(today.ToString()+ " - this is day. "+month.ToString()+ " - this is month. " + year.ToString() + " - this is year");

would be "10 - this is day. 3 - this is month. 2020 - this is year"

0

You can use DateTime.Now.ToShortDateString() like so:

var test = $"<b>Date of this report:</b> {DateTime.Now.ToShortDateString()}";
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
0

Code for Day:

lblyear.Text = DateTime.Now.ToString("DD");

Code for Month:

lblyear.Text = DateTime.Now.ToString("MM");

Code for Month name (Feb):

lblyear.Text = DateTime.Now.ToString("MMM");

Code for Year:

lblyear.Text = DateTime.Now.ToString("YYYY");
-1
string now = Convert.ToString(DateTime.Now.ToShortDateString());
Console.WriteLine(now);
Console.ReadLine();
LarsTech
  • 80,625
  • 14
  • 153
  • 225
montes
  • 7
  • 1
-2

for month

DateTime.Now.ToString("MM");

for day

DateTime.Now.ToString("dd");

for year

DateTime.Now.ToString("yyyy");
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
-3
DateTime.Now.ToString("dd/MM/yyyy");
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shahbaz Raees2
  • 201
  • 2
  • 5