DatePlus["12 May 2017",2]
gives
"Sun 14 May 2017 00:00:00"
How can I get just the "14 May 2017"
?
One can just set the value of $DateStringFormat
locally within a Block[]
, like so:
Block[{$DateStringFormat = Riffle[{"Day", "MonthNameShort", "Year"}, " "]},
DatePlus["12 May 2017", 2]]
"14 May 2017"
Block[{$DateStringFormat = Riffle[{"Day", "MonthNameShort", "Year"}, " "]},
DatePlus["12 May 2017", 42]]
"23 Jun 2017"
d = DatePlus["12 May 2017", 2];
DateList[d][[;; 3]]
{2017, 5, 14}
Or
DateObject @ DateList[d][[;; 3]]
"14 May 2017"
? I mean, OP asked for a string.
$\endgroup$
For explicit control over output, you may want to look at DateValue
;
e.g.
DateValue[DatePlus["12 May 2017", 2], {"Year", "Month", "Day"}]
{2017, 5, 14}
If you want month-name explicitly (but trickier possibly for comparisons):
DateValue[DatePlus["12 May 2017", 2], {"Year", "MonthName", "Day"}]
{2017, "May", 14}
If you're going to handle date comparisons (subtraction, let's say), you may want to leave it in DateObject
form, then convert; e.g.
DateObject @ DatePlus["17 May 2032", 15] -
DateObject @ DatePlus["12 May 2017", 2]
Quantity[5497, "Days"]
If you really want a string "for comparison" (string compare??), as Szabolcs commented, you can read about DateString
here:
DateString[DatePlus["12 May 2017", 2],
Riffle[{"Year", "MonthName", "Day"}, " "]]
"2017 May 14"
If you're going to be manipulating DateObject
's anyway, DateString
will inherit whatever format you give the DateObject
:
DateString[
DateObject[DatePlus["12 May 2017", 2],
DateFormat -> Riffle[{"Year", "MonthName", "Day"}, " "]]]
"2017 May 14"
If you are performing date calculations/comparisons it would be better to work with DateObject
than to work with string representations of dates. With strings you miss out on the many built-in features for handling dates; see Date Operations and Tests on Dates in the Date and Time.
DateObject
To explicitly convert a date string you would use DateObject
's string syntax, extract the DateValue
s and then feed that back into DateObject
for a "Day"
granularity date. It is a bit verbose to be explicit so create a pure function with this steps.
toDateObjectDay =
DateObject[
DateValue[DateObject[{#, {"Day", "MonthName", "Year"}}], {"Year", "Month", "Day"}],
"Day"] &;
String dates of this form can be converted to "Day"
DateObject
s.
dt = toDateObjectDay@"12 May 2017"
Notice that this date has "Day"
granularity and no time components since they were note chosen with DateValue
.
All date operations will preserve the date granularity.
DatePlus[dt, 2]
Even if the operation involves a lower granularity.
DatePlus[dt, {1, "Hour"}]
Using DateObject
s also allows you to use comparison operators.
stringDates = {"3 June 2014", "12 May 2017", "30 Sep 2017", "1 March 2022"};
dates = toDateObjectDay /@ stringDates
Select[# <= dt &]@dates
Select[Between[{Today, DatePlus[Today, {2, "Decade"}]}]]@dates
DateObject
s can be converted back to formatted strings with DateString
.
DateString[dt, Riffle[{"Day", "MonthNameShort", "Year"}, " "]]
"12 May 2017"
Hope this helps.
DateObject[{"12 May 2017", {"Day", "MonthName", "Year"}}]
returns the DateObject::str: String 12 May 2017 cannot be interpreted as a date.
error, while simple DateObject["12 May 2017"]
returns the expected output.
$\endgroup$
Commented
Oct 3, 2017 at 1:34
DateObject
was updated was in 11.1
$\endgroup$
toDateObjectDay@"12 May 2017"
returns a bunch of errors...
$\endgroup$
Commented
Oct 3, 2017 at 6:03
DateObject
as a result, not a string. What do you want to do with the result? $\endgroup$DateString
on theDateObject
(please look it up in the documentation). $\endgroup$"
marks, and I did not understand your question at all. I fixed it. What do you mean by "compare"? Please try to clarify your question. Do you want to decide which date is earlier? Do you want to format the date as a string? $\endgroup$StringDrop[#,-9]&@DatePlus["12 May 2017",2]
? $\endgroup$