Get the beginning of the PROVIDED week in a GlideDate/GlideDateTime

Mark Scott1
Giga Expert

Hello Community,

I have an interesting problem. I need to get the beginning of the week for a PROVIDED datetime:

var date = new GlideDate();

date.setValue('2017-06-08');

date.beginningOfWeek(); //This doesn't exist....

I'm already aware of gs.beginningOfThisWeek() but this only get the beginning of the week that I'm currently in. Before I go off the ledge and write a GlideDateTimeExtension script include, does anybody know of a way I can do this?

1 ACCEPTED SOLUTION

Mark Scott1
Giga Expert

In case anybody ever needs it, I wrote some functions to help extend the platform api. Please check the math before you blindly follow any of these. It works in my case but it might not work in yours!



getBeginningOfWeek: function(date) {


              var gdt = new GlideDateTime(date);


              gdt.addDays(-1 * gdt.getDayOfWeekUTC() + 1);


              return gdt;


      },


      getEndOfWeek: function(date) {


              var gdt = new GlideDateTime(date);


              gdt.addDays(7 - gdt.getDayOfWeekUTC());



              var gTime = new GlideTime();


              gTime.setValue("23:59:59");


              gdt.add(gTime);


              return gdt;


      },


  getEndOfLastWeek: function(date){


  var gdt = new GlideDateTime(date);


  gdt.addDays((-1 * gdt.getDayOfWeekUTC()));


  gs.log("getEndOfLastWeek - " + gdt);


  return this.getEndOfWeek(gdt);


  },



I'm going to be marking this answer as the correct one. I cannot find anywhere in the platform where this date math is provided.


View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

Hi Mark,



Try below



var date = new Date();


date.setValue('2017-06-08');


var first = date.getDate() - date.getDay(); // First day is the day of the month - the day of the week


var firstday = new Date(curr.setDate(first)).toUTCString();



gs.addInfoMessage(firstday );



Please mark this response as correct or helpful if it assisted you with your question.

Sanjiv,


Mechanically I get where to head. In fact, I'd rather use something like this:


var gdt = new GlideDateTime('2017-06-08');


gdt.addDays(-1 * gdt.getDayOfWeek());



And I know this will technically give me Monday instead of Sunday but I'm ok with this.


My question is more along the lines of this: Does the platform already have any of this built in?



With all the date/time functions, this seems like a big miss...


Mark Scott1
Giga Expert

In case anybody ever needs it, I wrote some functions to help extend the platform api. Please check the math before you blindly follow any of these. It works in my case but it might not work in yours!



getBeginningOfWeek: function(date) {


              var gdt = new GlideDateTime(date);


              gdt.addDays(-1 * gdt.getDayOfWeekUTC() + 1);


              return gdt;


      },


      getEndOfWeek: function(date) {


              var gdt = new GlideDateTime(date);


              gdt.addDays(7 - gdt.getDayOfWeekUTC());



              var gTime = new GlideTime();


              gTime.setValue("23:59:59");


              gdt.add(gTime);


              return gdt;


      },


  getEndOfLastWeek: function(date){


  var gdt = new GlideDateTime(date);


  gdt.addDays((-1 * gdt.getDayOfWeekUTC()));


  gs.log("getEndOfLastWeek - " + gdt);


  return this.getEndOfWeek(gdt);


  },



I'm going to be marking this answer as the correct one. I cannot find anywhere in the platform where this date math is provided.