Expand my Community achievements bar.

SOLVED

Datetime format in JSSP for dd-mm-yyyy

Avatar

Level 4

Hi Everyone,

We are encountering an issue regarding datetime in our current solution in both JSSP and Workflow. We are receiving "dd-mm-yyyy hh:mm:ss". However, it seems parsetimestamp or any other functions which is capable to format such datetime data.

Can anyone suggest how to use any javascript function to get timestamp from the above formatted data.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @supratim320 ,

i thought Date can parse date in any format but somehow DD-MM-YYYY is not one the formats it accepts. Same goes for the JSAPI function parseDateTime it accepts only ISO 8601 date strings.

You can still do a simple mapping:

 

var  dateString = '05-04-2023 10:30:00',
 parts = dateString.split(' '),
 dateParts = parts[0].split('-'),
 timeParts = parts[1].split(':'),

 year = parseInt(dateParts[2], 10),
 month = parseInt(dateParts[1], 10) - 1,
 day = parseInt(dateParts[0], 10),
 hours = parseInt(timeParts[0], 10),
 minutes = parseInt(timeParts[1], 10),
 seconds = parseInt(timeParts[2], 10), 
 date = new Date(year, month, day, hours, minutes, seconds);
logInfo(date);
var df = formatDate(date,"%4Y-%2M-%2D %02H:%02N:%02S");
logInfo(df);

//04/05/2023 4:39:10 AM	js	2023-04-05 10:30:00
//04/05/2023 4:39:10 AM	js	Wed Apr 05 2023 10:30:00 GMT-0400 (EDT)

 

 
Although I would recommend sending already preformatted date in ISO 8601, from the source system if it is possible 


Marcel Szimonisz

View solution in original post

8 Replies

Avatar

Community Advisor

Whats your requirement?

 

Show current output and expected output.

Avatar

Level 4

Hi @david--garcia , I am unable to save this data in Database.

Sample input as string column: "15-12-2023 05:40:15" (dd-mm-yyyy hh:mi:ss)

When I am running as: formatDate(orderDate,"%4Y-%2M-%2D %02H:%02N:%02S"), error: "invalid character at position 5 ('3')"

 

However, if I change datetime into: "2023-12-15 05:40:15" (yyyy-mm-dd hh:mi:ss), formatDate(orderDate,"%4Y-%2M-%2D %02H:%02N:%02S") works.

So, my question is: how can I save a date format which is received as: dd-mm-yyyy hh:mi:ss

 

Avatar

Community Advisor

Hello @supratim320 ,

you can use native JavaScript functionality to parse string into date:

 

var d = new Date("15-12-2023 05:40:15");//native JS
formatDate(d,"%4Y-%2M-%2D %02H:%02N:%02S")//adobe campaign jsapi

 

 

Marcel

Avatar

Level 4

Thank you for the update @Marcel_Szimonisz , but this output to me as: "0000-01-00 20:00:00".

Avatar

Community Advisor

Can you logInfo the date created from new date()?

Avatar

Level 4

Here you go @Marcel_Szimonisz  :

 

var d = new Date();
logInfo(d);

//logs: Wed Apr 05 2023 08:13:31 GMT+0000 (UTC)

Avatar

Correct answer by
Community Advisor

Hello @supratim320 ,

i thought Date can parse date in any format but somehow DD-MM-YYYY is not one the formats it accepts. Same goes for the JSAPI function parseDateTime it accepts only ISO 8601 date strings.

You can still do a simple mapping:

 

var  dateString = '05-04-2023 10:30:00',
 parts = dateString.split(' '),
 dateParts = parts[0].split('-'),
 timeParts = parts[1].split(':'),

 year = parseInt(dateParts[2], 10),
 month = parseInt(dateParts[1], 10) - 1,
 day = parseInt(dateParts[0], 10),
 hours = parseInt(timeParts[0], 10),
 minutes = parseInt(timeParts[1], 10),
 seconds = parseInt(timeParts[2], 10), 
 date = new Date(year, month, day, hours, minutes, seconds);
logInfo(date);
var df = formatDate(date,"%4Y-%2M-%2D %02H:%02N:%02S");
logInfo(df);

//04/05/2023 4:39:10 AM	js	2023-04-05 10:30:00
//04/05/2023 4:39:10 AM	js	Wed Apr 05 2023 10:30:00 GMT-0400 (EDT)

 

 
Although I would recommend sending already preformatted date in ISO 8601, from the source system if it is possible 


Marcel Szimonisz

Avatar

Community Advisor

Hi @supratim320 ,

Try the below script

var orderDate = "15-12-2023 05:40:15";
var yyyymmddFormat = orderDate.substr(6,4)+"-"+orderDate.substr(3,2)+"-"+orderDate.substr(0,2)+orderDate.substr(10,9);
var formatedOrderDate = formatDate(yyyymmddFormat,"%4Y-%2M-%2D %02H:%02N:%02S");

Hope this helps!