Sp-dev-docs: Column Formatting - Date Format

Created on 14 May 2018  路  8Comments  路  Source: SharePoint/sp-dev-docs

[x ] Question

I have a SharePoint list with a column containing dates with the following format : 3/15/2018
and i'd like with a JSON code to display 2018/03 instead

the below code is not working
{
"elmType": "div",
"txtContent": {
"operator": "String.format("{MM/YYYY}")",
"operands" : ["@currentField"]
}
}

can someone help me find the right syntaxe?
thanks in advance

community other question

All 8 comments

The dates from solutions are formatted based on the region settings of the web. If you go into site settings and then regional settings you will be able to set your date formatting.

Please see custom formatter reference
https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting

operator supported for date fields are

toLocaleString()
toLocaleDateString()
toLocaleTimeString()

Good question! Unfortunately, it is not currently possible to apply custom string formats or to extract portions of dates using column formatting.

However, you are able to do this with standard calculated columns:

=TEXT([YourField],"MM/YYYY")

You can also accomplish this with a SPFx Field Customizer.

What about when I use @currentField for a DateAndTime field and instead of only date also hours appear.

@Miteanea DateTime fields in SharePoint always have both date and time stored. However, you can choose in the field settings how they are displayed (date only or date and time) and this works great for standard display. However, this setting is not passed into list formatting. You are given the raw value and then choose how to display it.

To only show the date you can wrap it in the toLocaleDateString operator. For instance: "txtContent": "=toLocaleDateString(@currentField)"

If your question is more specific than that or if you are have other questions, please open a new issue. Thanks!

I used toLocaleDateString(@currentField)+' '+toLocaleTimeString(@currentField) to show date and time.
Thanks

I have a SharePoint column that has dates entered as well as other characters (alpha and underscore). I have had success with viralnaik77 suggestion: toLocaleDateString(@currentField) on the DATES in the field, but all the alpha and underscore error our. Is there a work around so that it formats anything that looks like a date but leaves the rest alone?

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikeparkie picture mikeparkie  路  3Comments

zerovectorspace picture zerovectorspace  路  3Comments

patrick-rodgers picture patrick-rodgers  路  3Comments

jonthenerd picture jonthenerd  路  3Comments

karishmaTCS picture karishmaTCS  路  3Comments