0

I want to get date of the last day of week with a specific timezone.

I got this code that works good but it shows the computer timezone. i want to add a specific timezone like GMT-0500

 {     
 var lastday = date.getDate() - (date.getDay() - 1) + 6;
 return new Date(date.setDate(lastday)); 
 }
dt = new Date(); 
dt.setHours(23,59,59,999);
document.getElementById("lastday").innerHTML = endOfWeek(dt).toString();
console.log(endOfWeek(dt).toString());

How can i change the timezone? please help.

DevzValley
  • 115
  • 1
  • 1
  • 11
  • @InsightCoders - Please clarify what you mean exactly by "last day of week". Do you mean Saturday or Sunday in all cases? Or do you expect the week start/end days to be determined by locale? – Matt Johnson-Pint Apr 20 '20 at 16:39
  • @mplungjan - If I'm reading the OP correctly, they're not asking how to set a specific time zone, but how to get the last day of a week based on the current time in a specific time zone. Those are two related, but still different things. – Matt Johnson-Pint Apr 20 '20 at 16:46
  • Heel read has code to set the last day of the week. Apply time zone first then do the thing already posted – mplungjan Apr 20 '20 at 16:53
  • @mplungjan - nope. The `Date` object can't do that. It can only manipulate time with regard to UTC or local time. See my answer below. Thanks. – Matt Johnson-Pint Apr 20 '20 at 16:54
  • So how come you answer with [code identical to the dupe I posted](https://stackoverflow.com/a/54453990/295783) ? – mplungjan Apr 20 '20 at 17:04
  • @mplungjan - dups are about the question, not the answers. But I suppose until the OP responds with more details about the "last day of the week" part, there's no point in answering. I deleted my answer until then. Thanks. – Matt Johnson-Pint Apr 20 '20 at 19:25
  • @MattJohnson-Pint I mark as dupe a question that has an answer in another question. If the main issue is X I don’t care that the title is not exact the same. This question is not about last day because that is already answered by OP, but about time zones – mplungjan Apr 20 '20 at 19:31
  • Form the above code i am getting the date of the last day of week(Sunday). the output is Sun Apr 26 2020 23:59:59 GMT+0600 (Bangladesh Standard Time). i want to change the timezone, so it will be Sun Apr 26 2020 23:59:59 GMT-0500 (US Standard Time). – DevzValley Apr 21 '20 at 17:11
  • did you try moment.js? https://momentjs.com/timezone/ – Lety Apr 22 '20 at 14:14
  • Hi. Sorry, but the `Date` object cannot do that. Also there's no such thing as "US Standard Time". (The US has many time zones). – Matt Johnson-Pint Apr 24 '20 at 17:52
  • See https://stackoverflow.com/a/15171030/634824 – Matt Johnson-Pint Apr 24 '20 at 17:57

0 Answers0