Parsing date and time in email action

samadam
Kilo Sage

I have date in the format: 

RequestedDate: October 04, 2024 1:45 PM

 

Is there away to parse this and split this format to populate data and time fields?

1 ACCEPTED SOLUTION

Shivalika
Mega Sage

Hello @samadam 

 

Please ignore my previous comment, try with below script, now it's giving accurate result - 

 

var requestedDateStr = "October 04, 2024 1:45 PM"; // Input string

 

// Convert the string into a Date object

var jsDate = new Date(requestedDateStr);

 

// Ensure the date is correctly parsed

if (!isNaN(jsDate.getTime())) {

    var gdt = new GlideDateTime();

    gdt.setValue(jsDate.toISOString().replace("T", " ").substring(0, 19)); // Convert to GlideDateTime format

 

    // Extract Date (YYYY-MM-DD)

    var dateOnly = gdt.getDate().getByFormat("yyyy-MM-dd");

 

    // Extract Time (HH:mm:ss)

    var timeOnly = gdt.getTime().getByFormat("HH:mm:ss");

 

    // Log or use these values

    gs.info("Date: " + dateOnly); // Output: 2024-10-04

    gs.info("Time: " + timeOnly); // Output: 13:45:00

} else {

    gs.info("Invalid date format!");

}

 

1000020670.png

 

1000020671.png

Now it's giving accurate result @samadam 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway,

 

Regards,

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwN

eEISQCY

View solution in original post

4 REPLIES 4

Shivalika
Mega Sage

Hello @samadam 

 

I just executed it based on the date you gave. Please try below script - 

 

var requestedDateStr = "October 04, 2024 1:45 PM"; // Input string

 

// Convert string to GlideDateTime

var gdt = new GlideDateTime();

gdt.setDisplayValue(requestedDateStr);

 

// Extract Date (YYYY-MM-DD)

var dateOnly = gdt.getDate().getByFormat("yyyy-MM-dd");

 

// Extract Time (HH:mm:ss)

var timeOnly = gdt.getTime().getByFormat("HH:mm:ss");

 

// Log or use these values

gs.info("Date: " + dateOnly); // Output: 2024-10-04

gs.info("Time: " + timeOnly); // Output: 13:45:00

 

1000020666.png

 

1000020667.png

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway,

 

Regards,

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Medi C
Giga Sage

Hi @samadam,

 

I don't think ServiceNow provides a function for that. Could you try the following:

function parseDateTime(dateTimeStr) {
    var months = {
        "January": "01", "February": "02", "March": "03", "April": "04",
        "May": "05", "June": "06", "July": "07", "August": "08",
        "September": "09", "October": "10", "November": "11", "December": "12"
    };

    // Extract components using regex
    var match = dateTimeStr.match(/^([A-Za-z]+) (\d{2}), (\d{4}) (\d{1,2}):(\d{2}) (AM|PM)$/);
    if (!match) {
        return "Invalid Date Format";
    }

    var month = months[match[1]];
    var day = match[2];
    var year = match[3];
    var hours = parseInt(match[4]);
    var minutes = match[5];
    var meridian = match[6];

    // Convert 12-hour format to 24-hour format
    if (meridian === "PM" && hours !== 12) {
        hours += 12;
    } else if (meridian === "AM" && hours === 12) {
        hours = 0;
    }

    // Construct the date string in YYYY-MM-DD HH:mm:ss format
    var formattedDateStr = year + "-" + month + "-" + day + " " + hours.toString().padStart(2, '0') + ":" + minutes + ":00";

    // Convert to GlideDateTime
    var gdt = new GlideDateTime();
    gdt.setValue(formattedDateStr); // Sets value in UTC


    return gdt;
}
var gdt = parseDateTime("January 12, 2027 12:23 AM");
gs.info(gdt.getValue());  // 2027-01-12 00:23:00

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Hi @samadam,

 

I believe my solution also work and meet your requirements. As per new ServiceNow Community feature, multiple solutions could be accepted. Please check and accept the solution if it works for you. 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Shivalika
Mega Sage

Hello @samadam 

 

Please ignore my previous comment, try with below script, now it's giving accurate result - 

 

var requestedDateStr = "October 04, 2024 1:45 PM"; // Input string

 

// Convert the string into a Date object

var jsDate = new Date(requestedDateStr);

 

// Ensure the date is correctly parsed

if (!isNaN(jsDate.getTime())) {

    var gdt = new GlideDateTime();

    gdt.setValue(jsDate.toISOString().replace("T", " ").substring(0, 19)); // Convert to GlideDateTime format

 

    // Extract Date (YYYY-MM-DD)

    var dateOnly = gdt.getDate().getByFormat("yyyy-MM-dd");

 

    // Extract Time (HH:mm:ss)

    var timeOnly = gdt.getTime().getByFormat("HH:mm:ss");

 

    // Log or use these values

    gs.info("Date: " + dateOnly); // Output: 2024-10-04

    gs.info("Time: " + timeOnly); // Output: 13:45:00

} else {

    gs.info("Invalid date format!");

}

 

1000020670.png

 

1000020671.png

Now it's giving accurate result @samadam 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway,

 

Regards,

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwN

eEISQCY