Forum Discussion
mwilliams71
Nov 19, 2021Brass Contributor
convert Date from YYYY-MM-DD to MM/DD/YYYY in jQuery/JavaScript
I'm attempting to use oListItem.set_item to insert date data into SharePoint from a custom form. I keep receiving "Request failed.String was not recognized as a valid DateTime. Essentially the date coming from the format of 2021-09-21 or (YYYY-MM-DD), it needs to be in the form of 09/21/2021(MM/DD/YYYY)
Any help appreciated.
mwilliams71 Try using:
var formattedDate = new Date("2021-09-21").toJSON();
OR
var tempDate = new Date("2021-09-21"); var formattedDate = [tempDate.getMonth() + 1, tempDate.getDate(), tempDate.getFullYear()].join('/');
Use formattedDate variable to set the date column using oListItem.set_item().
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- mwilliams71Brass ContributorI have 4 date fields, 2 that will be filled with a Date and 2 that will remain empty until later. I'm having a problem with 2 Date fields being empty at the time of submission. I get an error for the empty fields.
mwilliams71 Are those two date fields "Required" in column settings/list settings? If yes, you have to make them optional from list settings.
For empty date fields, you don't have to write ".set_item()" in your code.
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.