Return Date from Date Time FIeld

Posted by tehilac@safeguard.co.il on 31-May-2016 00:25

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

 



All Replies

Posted by Shiva Duriseati on 31-May-2016 01:19

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

Posted by Mohammed Siraj on 31-May-2016 01:27

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}');

Posted by tehilac@safeguard.co.il on 31-May-2016 03:45

Thanks you!!

This thread is closed