Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only the Date without Time from DateTime

Tags:

c#

i came across a situation that, i need to get only the Date out from DateTime.

i am having a DateTime? StartDate property (Nullable) used to hold the date value

i tried below,

var d = Convert.ToDateTime(StartDate).Date;

but its returning me d as eg. 6/22/2006 12:00:00AM

after doing var d = Convert.ToDateTime(StartDate).Date.ToString("d");

i'm able to get d as 6/22/2006..but i dont want to convert my DateTime? to String

is their any way to get only the Date without using the ToString("d")?

like image 872
user3085995 Avatar asked Mar 11 '14 08:03

user3085995


People also ask

How do I remove the time from a date in Python?

Using strfttime to Remove the Time from Datetime in Python We can use strftime() to easily remove the time from datetime variables. For example, if you want to print out the date in the format “YYYY-MM-DD”, we pass “%Y-%m-%d” to strfttime() and no time is printed.

How do I convert a DateTime to date in SQL?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.


3 Answers

Use the Date property to get the Date component of DateTime instance:

DateTime dateTimeNow = DateTime.Now;
DateTime datePartOnly = dateTimeNow.Date;    // Return 00/00/0000 00:00:00

With this approach, Date property will return the date at midnight. So the time part will be 00:00:00 in this case.

There are couple of alternate ways to get the just the Date part, but the return type of it will be a string:

1.) Using .ToString(string? format) where format can be standard or custom format string

string dateOnlyString = dateTimeNow.ToString("dd/MM/yyyy"); 
//Can also use .ToString("dd-MM-yyyy");

2.) Using .ToShortDateString() to return a culture sensitive date string

string dateOnlyString = dateTimeNow.ToShortDateString(); 
//Returns M/d/yyyy for "en-US" culture
//Returns yyyy/M/d for "ja-JP" culture

Reference: here.

like image 178
Saket Kumar Avatar answered Oct 19 '22 23:10

Saket Kumar


try this: string x = DateTime.Now.ToShortDateString().

this will get the date dd/mm/yy given to the string x.

like image 42
Ralph De Guzman Avatar answered Oct 20 '22 00:10

Ralph De Guzman


I think you question is sort of... moot.

You ask for a date without a time, but get a DateTime, which has both. I really don't think that should be a problem in most cases though:

If you create a DateTime with a certain date, and compare it to another date, and both of these have their time set to midnight, your comparisons will be valid and correct. Eg:

var yesterday = new DateTime(2014, 3, 10);
var today = new DateTime(2014, 3, 11);
var tomorrow = new DateTime(2014, 3, 12);

Comparing and sorting these will work as you expect, and so will the following:

if(today == DateTime.Today){
    Console.WriteLine("Today is the day!");
}

In other words, you should be perfectly fine just pretending like the time-part does not exist.

Also, as you touched upon yourself in the OP, you can use the property Date if you want to make sure to avoid any time-component:

// Note the addition of hours, minutes and seconds:
var today = new DateTime(2014, 3, 11, 14, 35, 33); 

if(today == DateTime.Today){
    Console.WriteLine("This never happened...");    
}

if(today.Date == DateTime.Today){
    Console.WriteLine("...But today is still the day!");    
}
like image 25
Kjartan Avatar answered Oct 19 '22 22:10

Kjartan