JSON to display date as mmm-yyyy is MS Lists without using a calculated column.

Carter, Miriam 21 Reputation points
2022-02-14T23:22:03.56+00:00

Hello, can you help me write JSON to display date as mmm-yyyy is MS Lists without using a calculated column?
I'm pretty sure this is possible but I'm a novice to both MS Lists and JSON and I haven't been able to make it work.

I need a date column to display as mmm-yyyy, for example 'Jan-2021.' I know its very easy to create a calculated column from another date column and display in this manner using a TEXT formula, however, I don't want to confuse my data-entry users by having one date column where they pick a date to enter their monthly results and then see that month displayed in a separate column. I know they will just try to click into the Month display and enter the month there instead of using the separate date picker column.

I found code that's close to what I need, but it just results in displaying the JSON code itself, not the actual results. I'm sure I'm doing something wrong but not sure what.

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "span",
"txtContent": "=if(getMonth([$Month-Year])==0,'Jan',if(getMonth([$Month-Year])==1,'Feb',if(getMonth([$Month-Year])==2,'Mar',if(getMonth([$Month-Year])==3,'Apr',if(getMonth([$Month-Year])==4,'May',if(getMonth([$Month-Year])==5,'Jun',if(getMonth([$Month-Year])==6,'Jul',if(getMonth([$Month-Year])==7,'Aug',if(getMonth([$Month-Year])==8,'Sept',if(getMonth([$Month-Year])==9,'Oct',if(getMonth([$Month-Year])==10,'Nov','Dec'))))))))))) + '-' + getYear([$Month-Year])"
}

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,741 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yi Lu_MSFT 17,461 Reputation points
    2022-02-15T02:59:21.353+00:00

    Hi @Carter, Miriam
    You could try the following code:

         {  
           "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",  
           "elmType": "span",  
           "txtContent": "=if(getMonth(@currentField)==0,'Jan',if(getMonth(@currentField)==1,'Feb',if(getMonth(@currentField)==2,'Mar',if(getMonth(@currentField)==3,'Apr',if(getMonth(@currentField)==4,'May',if(getMonth(@currentField)==5,'Jun',if(getMonth(@currentField)==6,'Jul',if(getMonth(@currentField)==7,'Aug',if(getMonth(@currentField)==8,'Sept',if(getMonth(@currentField)==9,'Oct',if(getMonth(@currentField)==10,'Nov','Dec'))))))))))) + '-' + getYear(@currentField)"  
         }  
    

    As a result, you will get the format:

    174287-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. H Bray 5 Reputation points
    2023-05-31T09:23:23.7+00:00

    I've tidied up the JSON a little to remove the "Dec-" when there is no date (instead says "No date") and for my preference of having MMMM, YYYY:

    {  
           "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",  
           "elmType": "span",  
           "txtContent": "=if(getMonth(@currentField)==0,'Jan, ',if(getMonth(@currentField)==1,'Feb, ',if(getMonth(@currentField)==2,'Mar, ',if(getMonth(@currentField)==3,'Apr, ',if(getMonth(@currentField)==4,'May, ',if(getMonth(@currentField)==5,'Jun, ',if(getMonth(@currentField)==6,'Jul, ',if(getMonth(@currentField)==7,'Aug, ',if(getMonth(@currentField)==8,'Sept, ',if(getMonth(@currentField)==9,'Oct, ',if(getMonth(@currentField)==10,'Nov, ',if(getMonth(@currentField)==11,'Dec. ','No date'))))))))))))  + getYear(@currentField)"  
         }
    

    You probably could conditionally add the ", " depending on whether there is a date or not to avoid duplication, but KISS :)

    1 person found this answer helpful.

  2. Davy 0 Reputation points
    2023-08-28T09:15:38.6366667+00:00

    Or you can try:

    {  
           "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
           "elmType": "span",
           "txtContent": "=if(Number([$Month-Year])==0, '', substring('JanFebMarAprMayJunJulAugSepOctNovDec',getMonth([$Month-Year])*3, (getMonth([$Month-Year])+1)*3) + '-' + getYear([$Month-Year]) )"
    }  
    
    0 comments No comments