Working example of cs.save() saving Date and time?

Anyone out there have a working example of cs.save() where you save a “Date and Time”?

I’ve been trying to get it to work, and while the save succeeds the results are a garbage date and time.

The cs.save() call is really simple:

var ts = new Date(2020, 04, 04, 11, 45, 00);	// month is an index so 04 = May
cs.log("Date and time = " + ts);

var result = cs.save
	(
		cs.ref("OCL_FEI_obj_fei_record"),
		{
			":id"	: fei_rec_id,
			"OCL_FEI_fld_fei_next_value"	: next_value,
			"OCL_FEI_fld_fei_last_reset"	: ts
		}
	);

cs.log("Save results = " + JSON.stringify(result));

This version results in the following in the Detective:

‘Date and time = Mon May 04 2020 11:45:00 GMT-0600 (Mountain Daylight Time)
Note that date and time are correct
Saved Record 1027 (OCL>Util> Formatted Entity Identifier) in 0.0053s for rule OCL>R&D> initialize new document
Event Record modified triggered on Record 1027 (OCL>Util> Formatted Entity Identifier)
Save results = {“status”:true,“records”:{“base”:{“record_id”:1027,“action”:“modified”,“fields”:{“:PRO0000130EBEEB1”:{“old”:“20”,“new”:21},“:PRO0000128EBEEB1”:{“old”:“0000-00-00 00:00:00”,“new”:“-0001-11-30 00:00:00”}}}},“subset_review”:null}’
Something that looks like a date has been saved, but it isn’t the right date!

Setting ts thus:
var ts = Date.now();

produces results that look a little more like a valid date, but not the right one:

Date and time = 1588617641295
Saved Record 1027 (OCL>Util> Formatted Entity Identifier) in 0.0038s for rule OCL>R&D> initialize new document
Event Record modified triggered on Record 1027 (OCL>Util> Formatted Entity Identifier)
Save results = {“status”:true,“records”:{“base”:{“record_id”:1027,“action”:“modified”,“fields”:{“:PRO0000130EBEEB1”:{“old”:“22”,“new”:23},“:PRO0000128EBEEB1”:{“old”:null,“new”:“52311-04-16 23:08:15”}}}},“subset_review”:null}

Setting TS to a mySql formatted date string results in a NULL.

So, I’m guessing that I am missing something simple, again.

Thoughts, examples, and loving dope-slaps greatly appreciated.

Regards,

Randy

Hi Randy

All values passed through one of the record persistence functions are expected in the ‘editable’ form - that is, what you’d see if you were to display the field on a form with an editable field.

Typically, this defaults as dd/mm/yy and can be found under Locale > Defaults or via

cs.config('date_format')

Hope that helps!
Mark

To add to the above, be aware that if/when using raw timestamps within Create, they are expected in seconds, e.g 1588669298 whereas within JavascriptLand via Date.now() you’ll get the timestamp back in milliseconds - hence the year of 52311 you’re seeing there :slight_smile:

A quick division before passing the timestamp to Create will set you on your way.

Thanks Mark. I’ll give both a try.

I didn’t mention it, but I had tried passing the Date through the “String to editable date” Field Processor, with no joy. I’ll play with that again as well.

Regards,

Randy

Another option is to convert date formats - I have had loads of fun getting these correct. For cs.save, date format needs to be dd/mm/yyyy with leading zeros as appropriate. I use the following function:

function date_to_create (dt) {
let dx = (“0” + dt.getDate()).slice(-2) + “/” + (“0” + (dt.getMonth() + 1)).slice(-2) + “/” + dt.getFullYear();
return dx;
}

just use date_to_create(ts) in place of ts.