Created
August 21, 2019 16:52
-
-
Save seanrankin/1515a67166f1cba4c1e9c5f39db351f1 to your computer and use it in GitHub Desktop.
formatDateToString.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function formatDateToString(date, format){ | |
| const newDate = new Date(date); | |
| const D = newDate.getDate(); | |
| const DD = (newDate.getDate() < 10 ? '0' : '') + newDate.getDate(); | |
| const M = newDate.getMonth() + 1; | |
| const MM = ((newDate.getMonth() + 1) < 10 ? '0' : '') + (newDate.getMonth() + 1); | |
| const YYYY = newDate.getFullYear(); | |
| switch (format) { | |
| // Used in spreadsheet | |
| case "M/D/YYY": | |
| return (M + "/" + D + "/" + YYYY); | |
| break; | |
| // Used in the IGMS API | |
| case "YYYY-MM-DD": | |
| return (YYYY + "-" + MM + "-" + DD); | |
| break; | |
| default: | |
| return (M + "/" + D + "/" + YYYY); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment