Datetime format in JSSP for dd-mm-yyyy | Community
Skip to main content
Level 5
April 4, 2023
Solved

Datetime format in JSSP for dd-mm-yyyy

  • April 4, 2023
  • 4 replies
  • 3636 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Marcel_Szimonisz

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

4 replies

david--garcia
Level 10
April 4, 2023

Whats your requirement?

 

Show current output and expected output.

Level 5
April 4, 2023

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

 

Marcel_Szimonisz
Community Advisor
Community Advisor
April 4, 2023

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

Level 5
April 5, 2023

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

Marcel_Szimonisz
Community Advisor
Community Advisor
April 5, 2023

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

Marcel_Szimonisz
Community Advisor
Marcel_SzimoniszCommunity AdvisorAccepted solution
Community Advisor
April 5, 2023

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

ParthaSarathy
Community Advisor
Community Advisor
April 5, 2023

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!

~  ParthaSarathy S~  Click here to join ADOBE CAMPAIGN USER GROUP for Quarterly In-person | Hybrid | Virtual Meetups