Get monday of the week from the user input

anupama8
Tera Expert

Hi All,

I have developed a time card record producer form where user enters the time worked and select a date in the field "week_starts".

As per our req , this date should be Monday of the week that user enters the date. I have written a script to change the date to Monday of the same week (as the date input by user). But the problem is when we change the TimeZone. I am using getDayOfWeekLocalTime() . But in IST if Monday=1,sunday=7 then in EST it changes to Monday=7, So my logic fails in this case.

Here is my code:

var wdate = new GlideDateTime(producer.week_starts);

var finalDate = new GlideDateTime();

finalDate = getMonday(wdate);

current.week_starts_on = finalDate;

function getMonday(wdate,firstDay){

  if(day==2){

  temp.addDaysLocalTime(-1);

  }

  else if(day==3){

  temp.addDaysLocalTime(-2);

  }

  else if(day==4){

  temp.addDaysLocalTime(-3);

  }

  else if(day==5){

  temp.addDaysLocalTime(-4);

  }

  else if(day==6){

  temp.addDaysLocalTime(-5);

  }

  else if(day==7){

  temp.addDaysLocalTime(-6);

  }

  else{

  return wdate.getLocalDate();

  }

  return temp.getLocalDate();

}

Can someone suggest any modification or if someone has better idea to approach this requirement? It will be really helpful.

6 REPLIES 6

Deepa Srivastav
Kilo Sage

Hi ,



Instead of doing manupulations after entering the date , can it be changed like - when user chooses a date you can check if its Monday and if its not Prompt user like "select date which fall on Monday"...



do you think that will be better and easy to implement?



-----


Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.



Thanks,
Deepa


The question still remains how do i check for Monday?


Can you please try this:-



isFirstDayOfWeek(Object)   - Checks whether the date is the first day of the week. This uses the ISO standard of Monday being the first day of the week.



For more info check the below link:-



GlideSystem Date and Time Functions - ServiceNow Wiki



-----


Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.



Thanks,
Deepa


Anupama is it solved now?



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.