There may be times when you want/need to format a date field within Infoplus. By default, date fields are returned in the following format: (Year-Month-DayHour-Minute-Second) 2019-11-02T00:00:00.000Z. While this is useful for some purposes, there may be times you want to use a short or custom date format.
Due to the ever-changing nature of writing and maintaining scripts, Infoplus does not provide support in this area. We recommend someone in your IT Department handles scripting requests internally so that you keep all control over the process.
We have two simple solutions you can use to format your dates any way you want.
The first method is to use .toLocaleDateString
(new Date()).toLocaleDateString('en-US');
Simply place the date you want to format in the open parentheses of the new Date object.
var dateToFormat = record.DateToFormat; // or record.customFields.get("DateToFormat");
var currentDate = new Date(dateToFormat); //use your date here
currentDate.toLocaleDateString('en-US'); // "en-US" gives date in US Format - mm/dd/yy
The second method is to use .getDate(), .getMonth() and .getYear() to create your own custom format.
var startDate = record.dateField // or record.customFields.get("dateField"); var convertedStartDate = new Date(startDate); var month = convertedStartDate.getMonth() + 1 var day(date) = convertedStartDate.getDate(); var year = convertedStartDate.getFullYear(); var shortStartDate = month + "/" + day + "/" + year;
Be mindful of using .getDate vs .getDay
Here are more date methods that can be used:
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |