Column formatting in SharePoint has been around for a while but today I was asked about making how to format date columns to appear appear like yyyy-MM-dd.

The requirements

In my SharePoint library I’ve got a date field that currently displays as 03/06/2025 (3 June 2025), now we want to display this as 2025-06-03. Yes you could update your locale settings in your site for some reformatting but there are some limitations with that as well.

My SharePoint site with a date column
My SharePoint site with a date column

Functions needed to format date columns

For the solution we need a few functions. For the full reference to the available functions please check out the Microsoft documentation. If only we had a formatdatetime function like we have in Power Automate and Power Apps. Unfortunately we will have to do a bit more work in column formatting.

GetMonth – this will get the month number from a date column

GetDate – this will get the day number from a date column

GetYear – gets the year from the date column.

If – allows you to generate output depending on a condition

Length – checks the length of a text

ToString – converts numbers or date data in to text fields

Get the current column

As we want to format a column we could reference the column using the following syntax: [$ColumnName]

However to get to the current column you can also use @CurrentField. This means that if you want to apply this field formatting to your lost you can simply copy the code I have included in this post. For all examples I have used the current field reference.

Formatting the date column

Now that we have looked at the functions and column names, we can create the full column formatting json script. Please notice that the txtContent property doesn’t allow any formatting. all the json has to be included in a single text without any new lines.

{  
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",  
  "elmType": "span",  
  "txtContent": "=if(length(toString(@currentField)) == 0, '', toString(getYear(@currentField)) + '-' +  toString(getMonth(@currentField)+1) + '-' + toString(getDate(@currentField)) )"
}  

Once I apply this in the settings to my date field, I’m seeing the new data format appear as we want the dates to appear. The initial check for the length of the data field is to make sure that we haven’t got an empty value. Empty values we simply want to appear as an empty text. If you prefer to display a text like No date provided then that is possible as well. Simply replace the ‘ ‘ with whatever text you like to appear.

Format date columns in SharePoint
Format date columns in SharePoint

Adding leading zeros

In the above screenshot we can see that we are nearly there. In our example we can see that the day and the month numbers appear without leading zeros.

To get the leading zeros into the month and date. This is why I used the slightly confusing date of 03/06 ( this is 6 March in the US and 3 June in the rest of the world)

To get the leading zeros in the following small update to our code will help:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "span",
  "txtContent": "=if(length(toString(@currentField)) == 0, '', toString(getYear(@currentField)) + if(getMonth(@currentField) > 9,'-', '-0') +   toString(getMonth(@currentField)+1),  + if(getDate(@currentField) > 9,'-', '-0') + toString(getDate(@currentField)) )"
}

This change results in the date column being formatted as shown below in my library

Format date columns with leading zeros in SharePoint
Format date columns with leading zeros in SharePoint

Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

5 thoughts on “Format Date columns in SharePoint

  1. When I tried the second script, for the leading 0s, it also added 0s to 10, So I got dates like 2025-010-17. I tried to fix it by replacing the >9 in the script with >10, but that didn’t work-I don’t know JSON, so I was just guessing.

      1. I copied and pasted your second script exactly, and that’s where it gave me -010 instead of -10

  2. There’s a ‘getMonth(@currentField)+1’ in there so, when you have a date in September, it adds a leading zero and then adds 1 to the month, so you get ‘-010’. I removed the ‘+1’.

  3. Hi all. After encountering the same problem as reported above (October reported as “-010”), removing the ‘+1′ resulted in wrong month reported for all dates (“00” for January, “01” for Feb….).
    For whatever reason, changing the trigger for applying the extra “0” from September (9) to August (8) seems to solve it elegantly (all months reported correctly, with extra “0” for Jan. to Sep. – included.

    i.e. in my case, the following JSON code works perfectly:

    {
    “$schema”: “https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json”,
    “elmType”: “span”,
    “txtContent”: “=if(length(toString(@currentField)) == 0, ”, toString(getYear(@currentField)) + if(getMonth(@currentField) > 8,’-‘, ‘-0′) + toString(getMonth(@currentField)+1), + if(getDate(@currentField) > 8,’-‘, ‘-0’) + toString(getDate(@currentField)) )”
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.