Change date format to yyyy-mm-dd

I have some dates currently formatted as "2016 May 20, Friday". I would like to change the date format to "2016-05-20". How can I do this for 200 cells at once? I have tried selecting the cells and going to Inspector -> Cell -> Data Format -> Date & Time, but after choosing my date format, nothing changes. Help?


I would also like the menu bar command Insert -> Date & Time to use the yyyy-mm-dd format by default, so I don't have to reformat the date every time I insert it.

Posted on May 20, 2016 4:02 PM

Reply
25 replies

May 20, 2016 9:53 PM in response to ffiti

To fix your 200 or so cells you can try this:


  1. Make a backup copy of your work first!
  2. Copy-paste the script below into Script Editor (in Applications > Utilities)
  3. Select the 200 cells containing the dates in the format you posted. (This assumes they're all in that same format).
  4. With the cells selected, click the triangle 'run' button in Script Editor.


This should convert the cells to true date-time strings displayed in YYYY-M-D format or as text in YYYY-MM-DD format. Then go to Data Format panel and format them as YYYY-MM-DD.


If the script throws off an error post with specifics and I will modify. You should first check that you have Script Editor checked at System Preferences > Security & Privacy > Privacy > Accessibility.


Note that you don't need AppleScript knowledge or experience to try this, but I'll need specifics of the problem if you encounter one.


SG


tell application "Numbers"

tell front document's active sheet

tell (first table whose selection range's class is range)

repeat with c in (get selection range)'s cells

set v to c's value

set yyyy to v's first word

set dd to v's third word

set mm to my monthNameToNum(v's second word)

set c's value to yyyy & "-" & mm & "-" & dd

end repeat

end tell

end tell

end tell


to monthNameToNum(theMonth)

set monthList to {January, February, March, April, May, June, July, August, September, October, November, December}

repeat with i from 1 to 12

if theMonth = ((monthList's item i) as string) then

set monthNum to (text -2 thru -1 of ("0" & i))

return monthNum

end if

end repeat

end monthNameToNum

May 20, 2016 11:37 PM in response to ffiti

"Data Format" an "Alignment" are two different settings.


User uploaded file

Data Format is a setting in the Cell pane of the Format Inspector. It choses what Numbers 'thinks' is the most appropriate format for the data contained in the cell.

User uploaded file

Alignment is a setting in the Text pane of the Format Inspector. Left on Automatic, as shown here, Numbers will align numeric, Date & Time, and Duration values to the right edge of the cell, and Text values to the left. Any of the other four settings will override the Automatic choice, as they have done in the three 'date' examples in the screen shot you posted.


The two controls in the second row of Alignemnt decrease or increase the indent of text.


The three in the third row place text at the top, centre or bottom of the cell, and the check box makes text too long to it in the width of the cell wrap to new lines (and expand the cell—and the rest of the row—vertically to accommodate all of the text.


"Text" in this context means the string of characters used to represent a value in the cell, and includes values of all types.


Regards,

Barry

May 21, 2016 6:35 AM in response to ffiti

Hi ffiti,


Back to your original question:

I have some dates currently formatted as "2016 May 20, Friday". I would like to change the date format to "2016-05-20". How can I do this for 200 cells at once?

Numbers is cluey at recognising anything that looks like a date string, even if it is text.


Try this:

User uploaded file

Formula in B2 for the year (and Fill Down)

=LEFT(A2,4)


Formula in C2 for the short month name (and Fill Down)

=MID(A2,6,3)


Formula in D2 for the month number (and Fill Down)

=MONTH(C2)


Formula in E2 for the day of the month (and Fill Down)

=MID(A2,10,2)


Formula in F2 to create a text date (and Fill Down)

=B2&"-"&C2&"-"&E2


Values in Column G are entered by typing.


Formula in H2 (and Fill Down) and this is where Numbers is clever enough to recognise a Text string that looks like a valid date 🙂.

=F2+G2


Regards,

Ian.

May 21, 2016 12:24 PM in response to ffiti

ffiti wrote:


The AppleScript correctly reformatted my 200 cells.


The other caveat: apparently, the Insert -> Date & Time menu bar command always breaks the Data Format setting and inserts a mutated date and time that isn't affected by the Data Format setting.


Glad the script successfully accomplished the one-time fix. Thanks for the green tick!


You can control the format you get from Insert > Date & Time menu inserts by changing the "Full:" format at System Preferences > Language & Region > Advanced ... > Dates:


User uploaded file


On my machine when I change that (I took out the Tuesday) Numbers uses the new format.


If you need to leave your existing format there for other apps, then I can post a link to an Automator Service that will paste in the current date in YYYY-MM-DD format. With the Service you would choose from the Numbers > Services menu rather than Insert menu.


SG

May 20, 2016 6:08 PM in response to ffiti

ffiti wrote:


I have some dates currently formatted as "2016 May 20, Friday". I would like to change the date format to "2016-05-20".


Are the dates formatted as "2016 May 20, Friday" right-aligned in the cells or left-aligned?


Right-aligned would mean Numbers is recognizing them as true date-time. Left-aligned would mean they're text.


If they're mostly right-aligned with some left-aligned, then try reentering the left-aligned ones.


If they're all left-aligned, then a script might be an efficient way to convert them to "2016-05-20".


SG

May 20, 2016 10:00 PM in response to SGIII

The AppleScript correctly reformatted my 200 cells.


For some reason it left the Data Format as Automatic, and when I changed it to Date & Time, it chose a strange "5, 2016" format for the date. But I selected all the cells and set them all to the correct Data Format, everything got fixed.


The other caveat: apparently, the Insert -> Date & Time menu bar command always breaks the Data Format setting and inserts a mutated date and time that isn't affected by the Data Format setting. As long as I manually type the date, it will be correctly formatted from now on.


Thanks again!

May 20, 2016 10:09 PM in response to ffiti

"Thanks for the response. I don't see what the alignment has to do with it; I just played around with left-, center-, and right-alignment and Numbers still refuses to reformat my dates. The Data Format for all the cells is Automatic."


With Alignment set to Automatic, Numbers will align numbers and number-like values (including Date & Time values and Durations) to the right, and Text values to the left. What SG was looking for here was a screen shot of the 'dates' with alignment left at 'Automatic' so that he could quickly see whether Numbers was recognizing these values as Date & Time values, or saw them simply as text. Unfortunately, that information was hidden by the three cells having their alignments set to Left, Center and Right.


Regards,

Barry

May 21, 2016 9:34 AM in response to ffiti

"I know what the difference is. Whatever gave you the idea I was confusing the two?"


Your responses in your two previous posts.


"I don't see what the alignment has to do with it; I just played around with left-, center-, and right-alignment and Numbers still refuses to reformat my dates. The Data Format for all the cells is Automatic."


"I know that Numbers automatically aligns cells depending on their contents. But in my case, every cell had Data Format "Automatic", and every cell was centered."


Plus the screen shot earlier showing three 'dates' with three different alignments.


Regards,

Barry

May 21, 2016 12:59 PM in response to ffiti

ffiti wrote:


Whatever gave you the idea I was confusing the two?


Just a general comment about something you no doubt already know from helping out people elsewhere in the ASC.


It's often not so easy to know what a new poster knows or doesn't know about Numbers in particular. Date-time strings and durations in Numbers are a specialized topic. They don't work the same as in Excel or other apps. There are surprising little things, such as Ian's point that a cell does not have to be formatted as 'Automatic' or 'Date & Time' for Numbers to treat the cell contents as a date-time. I was surprised, for example, to discover that when entering a month name such as April the default result is the first second of the first day of April, even though I might have meant text, as in the woman's name April. To prevent that I can type an apostrophe first (e,g., 'April ) and Numbers will treat it as true text. But, I can still format it as a date if I want!


So, even if the other person may be an expert, we've found here that it often helps when discussing Numbers to keep things basic and show screenshots if possible. That is no reflection on the expertise of other person. And it can help later readers of the thread, who may not be as experienced with the app.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Change date format to yyyy-mm-dd

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.