How to find day of the week in service now

basantsoni
Kilo Guru

Hello Friends,

Could you please help us here, we have one schedule job that runs on daily basis and inside this scheduled job we have condition to trigger notification only on Wednesday. So, to achieve this we have write a script that will tell us the day of the week and it’s return a number for a day of the week.

But, I need some confirmation, is this script giving us correct results?

Please provide yours comments on this.

var todayDateTime = new GlideDateTime(gs.nowDateTime());

var integerValForToday = todayDateTime.getDayOfWeekLocalTime();

gs.print(integerValForToday);

gs.print(todayDateTime.getLocalDate());

it’s return below result :

*** Script: 4
*** Script: 2018-05-24

 

Thanks, Basant

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Basantsoni,

Update your script as below:

var todayDateTime = new GlideDateTime(gs.nowDateTime());

var integerValForToday = todayDateTime.getDayOfWeekLocalTime();

var jsonObj = '{"1":"Monday","2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday","7":"Sunday"}';

var parser = new JSONParser();
var parsedData = parser.parse(jsonObj);

var dayOfWeek = parsedData[integerValForToday];

the highlighted variable will give you the value such as Monday or Tuesday etc

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Archana Reddy2
Tera Guru

Hi,

When I went through the below one, I got to know that the result cannot be Monday/Tuesday. It will be 1/2.

find_real_file.png

Thanks,

Archana

you can try like this.

 

 

<script>
function myFunction() {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];
document.getElementById("demo").innerHTML = n;
}
</script>

 

https://www.w3schools.com/jsref/jsref_getday.asp

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Basantsoni,

Update your script as below:

var todayDateTime = new GlideDateTime(gs.nowDateTime());

var integerValForToday = todayDateTime.getDayOfWeekLocalTime();

var jsonObj = '{"1":"Monday","2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday","7":"Sunday"}';

var parser = new JSONParser();
var parsedData = parser.parse(jsonObj);

var dayOfWeek = parsedData[integerValForToday];

the highlighted variable will give you the value such as Monday or Tuesday etc

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader