convert Date from YYYY-MM-DD to MM/DD/YYYY in jQuery/JavaScript

%3CLINGO-SUB%20id%3D%22lingo-sub-2986002%22%20slang%3D%22en-US%22%3Econvert%20Date%20from%20YYYY-MM-DD%20to%20MM%2FDD%2FYYYY%20in%20jQuery%2FJavaScript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2986002%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20attempting%20to%20use%20oListItem.set_item%20to%20insert%20date%20data%20into%20SharePoint%20from%20a%20custom%20form.%20I%20keep%20receiving%20%22Request%20failed.String%20was%20not%20recognized%20as%20a%20valid%20DateTime.%20Essentially%20the%20date%20coming%20from%20the%20format%20of%202021-09-21%20or%20(YYYY-MM-DD)%2C%20it%20needs%20to%20be%20in%20the%20form%20of%2009%2F21%2F2021(MM%2FDD%2FYYYY)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2988113%22%20slang%3D%22en-US%22%3ERe%3A%20convert%20Date%20from%20YYYY-MM-DD%20to%20MM%2FDD%2FYYYY%20in%20jQuery%2FJavaScript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2988113%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1001788%22%20target%3D%22_blank%22%3E%40mwilliams71%3C%2FA%3E%26nbsp%3BTry%20using%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Evar%20formattedDate%20%3D%20new%20Date(%222021-09-21%22).toJSON()%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOR%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Evar%20tempDate%20%3D%20new%20Date(%222021-09-21%22)%3B%0Avar%20formattedDate%20%3D%20%5BtempDate.getMonth()%20%2B%201%2C%20tempDate.getDate()%2C%20tempDate.getFullYear()%5D.join('%2F')%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EUse%20%3CSTRONG%3EformattedDate%26nbsp%3B%3C%2FSTRONG%3Evariable%20to%20set%20the%20date%20column%20using%20%3CSPAN%3EoListItem.set_item()%3C%2FSPAN%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CHR%20%2F%3E%3CP%3EPlease%20click%26nbsp%3B%3CSTRONG%3EMark%20as%20Best%20Response%3C%2FSTRONG%3E%26nbsp%3B%26amp%3B%20%3CSTRONG%3ELike%3C%2FSTRONG%3E%20if%20my%20post%20helped%20you%20to%20solve%20your%20issue.%20This%20will%20help%20others%20to%20find%20the%20correct%20solution%20easily.%20It%20also%20closes%20the%20item.%20If%20the%20post%20was%20useful%20in%20other%20ways%2C%20please%20consider%20giving%20it%20%3CSTRONG%3ELike%3C%2FSTRONG%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2992040%22%20slang%3D%22en-US%22%3ERe%3A%20convert%20Date%20from%20YYYY-MM-DD%20to%20MM%2FDD%2FYYYY%20in%20jQuery%2FJavaScript%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2992040%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1001788%22%20target%3D%22_blank%22%3E%40mwilliams71%3C%2FA%3E%26nbsp%3BAre%20those%20two%20date%20fields%20%22Required%22%20in%20column%20settings%2Flist%20settings%3F%20If%20yes%2C%20you%20have%20to%20make%20them%20optional%20from%20list%20settings.%3C%2FP%3E%3CP%3EFor%20empty%20date%20fields%2C%20you%20don't%20have%20to%20write%20%22%3CSPAN%3E.set_item()%22%20in%20your%20code.%3C%2FSPAN%3E%3C%2FP%3E%3CHR%20%2F%3E%3CP%3EPlease%20click%26nbsp%3B%3CSTRONG%3EMark%20as%20Best%20Response%3C%2FSTRONG%3E%26nbsp%3B%26amp%3B%20%3CSTRONG%3ELike%3C%2FSTRONG%3E%20if%20my%20post%20helped%20you%20to%20solve%20your%20issue.%20This%20will%20help%20others%20to%20find%20the%20correct%20solution%20easily.%20It%20also%20closes%20the%20item.%20If%20the%20post%20was%20useful%20in%20other%20ways%2C%20please%20consider%20giving%20it%20%3CSTRONG%3ELike%3C%2FSTRONG%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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.

 

 

 

 

5 Replies

@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.

I 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.

Thanks for the reply ganesshsanap. No. they are not required. However, a month later when the Status of the form change to approved, I will need to allow the user to go to the form and submit those dates.

-Mike
I got it to work, now it always gives me a day behind.