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")
?
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.
To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.
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.
try this:
string x = DateTime.Now.ToShortDateString()
.
this will get the date dd/mm/yy given to the string x.
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!");
}
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