2
$\begingroup$

DatePlus["12 May 2017",2]

gives

"Sun 14 May 2017 00:00:00"

How can I get just the "14 May 2017"?

$\endgroup$
5
  • $\begingroup$ I'm not sure what you mean. It gives a DateObject as a result, not a string. What do you want to do with the result? $\endgroup$
    – Szabolcs
    Sep 4, 2017 at 20:08
  • $\begingroup$ I mean how can I get "14 May 2017" from the result. I want to compare it with another date. $\endgroup$
    – user51827
    Sep 4, 2017 at 20:09
  • 1
    $\begingroup$ Do you want a string? If so, use DateString on the DateObject (please look it up in the documentation). $\endgroup$
    – Szabolcs
    Sep 4, 2017 at 20:17
  • $\begingroup$ You left off the " 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$
    – Szabolcs
    Sep 4, 2017 at 20:18
  • $\begingroup$ Is there an issue with StringDrop[#,-9]&@DatePlus["12 May 2017",2]? $\endgroup$ Sep 4, 2017 at 20:38

4 Answers 4

2
$\begingroup$

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"
$\endgroup$
1
$\begingroup$
d = DatePlus["12 May 2017", 2];

DateList[d][[;; 3]]

{2017, 5, 14}

Or

DateObject @ DateList[d][[;; 3]]

enter image description here

$\endgroup$
1
  • $\begingroup$ How is that "14 May 2017"? I mean, OP asked for a string. $\endgroup$
    – Kuba
    Sep 4, 2017 at 20:17
1
$\begingroup$

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"

$\endgroup$
0
$\begingroup$

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.

Convert to DateObject

To explicitly convert a date string you would use DateObject's string syntax, extract the DateValues 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" DateObjects.

dt = toDateObjectDay@"12 May 2017"

Mathematica graphics

Notice that this date has "Day" granularity and no time components since they were note chosen with DateValue.

Date Operations & Comparisons

All date operations will preserve the date granularity.

DatePlus[dt, 2]

Mathematica graphics

Even if the operation involves a lower granularity.

DatePlus[dt, {1, "Hour"}]

Mathematica graphics

Using DateObjects also allows you to use comparison operators.

stringDates = {"3 June 2014", "12 May 2017", "30 Sep 2017", "1 March 2022"};
dates = toDateObjectDay /@ stringDates

Mathematica graphics

Select[# <= dt &]@dates

Mathematica graphics

Select[Between[{Today, DatePlus[Today, {2, "Decade"}]}]]@dates

Mathematica graphics

Formatted Strings

DateObjects can be converted back to formatted strings with DateString.

DateString[dt, Riffle[{"Day", "MonthNameShort", "Year"}, " "]]
"12 May 2017"

Hope this helps.

$\endgroup$
6
  • $\begingroup$ Which version do you use? With Mathematica 11.2.0 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$ Oct 3, 2017 at 1:34
  • $\begingroup$ @AlexeyPopkov Yes, version 11.2.0 and I don't have 11.1.1 installed at the moment but I think this works in 11.1 as well. Last time DateObject was updated was in 11.1 $\endgroup$
    – Edmund
    Oct 3, 2017 at 2:09
  • $\begingroup$ I mean toDateObjectDay@"12 May 2017" returns a bunch of errors... $\endgroup$ Oct 3, 2017 at 6:03
  • $\begingroup$ @AlexeyPopkov All the code above works in 11.2 $\endgroup$
    – Edmund
    Oct 3, 2017 at 10:38
  • $\begingroup$ It doesn't work for me on Windows 7 x64: screenshot. $\endgroup$ Oct 3, 2017 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.