Parse Date (String) to only capture year, month, & day

JJG2
Kilo Explorer

I have a date in string form from an outside source: 2022-01-06T08:31:35

I need to parse this date to only capture 2022-01-06. Everything else can be thrown out. How can I script this?

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Not sure if this is 100% correct, have to search... though it does work.

var input = ' 2022-01-06T08:31:35';
var gdt = new GlideDateTime(input).getDate();

gs.info(gdt);

*** Script: 2022-01-06

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Not sure if this is 100% correct, have to search... though it does work.

var input = ' 2022-01-06T08:31:35';
var gdt = new GlideDateTime(input).getDate();

gs.info(gdt);

*** Script: 2022-01-06

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

vkachineni
Kilo Sage
Kilo Sage
var input = "2022-01-06T08:31:35";
var datePart = input.split("T")[0];
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Ct111
Tera Sage

Hello , Try something like this for example . Not tested but give a try by running on background script.

Example:

var date = new Date("2013-03-10T02:00:00Z");

date.toISOString().substring(0, 10)

 

Incase the Date doesn't work use the servicenow one GlideDate()

 

Mark my Answer as Correct and Helpful if it helps