Hello!
I want to return the date from the Create At field and to insert the date to different field (with trigger).
I used this formula:
function parseDate(str) {
var mdy = str.split('-');
return new Date(mdy[0], mdy[1]-1, mdy[2]);
}
var dt=parseDate('{!createdAt#iso}');
dt;
and the result is always:
Wed Dec 31 1969
Create At Field is a date time field.
I'd love to know what to change in the formula to accommodate to this type of field.
attach screen shot.
Thanks!
Tehila
Hi Tehila,
If your requirement is to get date from date/time field and to store that date into differect field or object. Instead of parsing the date you can directly use the following function:
Here I am creating a date field in object "Sample" and assigning the createdAt to DateField.
rbv_api.createRecord('Sample', {'DateField':new Date("{!createdAt}")});
Regards,
Shiva
In your above stated approach, Date details like hours, minutes & seconds are lost in parsing and instantiating a new Date object. We should ideally use the following Date constructor to instantiate new Date object from individual Date units:
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
A more simpler approach to parse date string literal into Date object is:
new Date('{!createdAt#js}'); //do not use iso format, JS Date.parse function will not be able to parse it correctly
Modified example:
function parseDate(str) {
return new Date(str);
}
var dt = new Date('{!createdAt#js}');
// or
var dt=parseDate('{!createdAt#js}');
Thanks you!!