-4

So I'm retrieving two values from a service which provide me with:

start date/time: 1970-06-08T23:00:00Z

end date/time: 1970-06-13T07:00:00Z

for example: tue-sat 08:00 - 16:00 | sun-thur 09:00 - 17:00

any ideas how to work this out?

1 Answers1

0

You can use getUTCDay():

<p id="demo"></p>
<script>
function setDayRange() {
    var dstart = new Date('1970-06-08');
    var dend = new Date('1970-06-13');
    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 nstart = weekday[dstart.getUTCDay()];
    var nend = weekday[dend.getUTCDay()];
    document.getElementById("demo").innerHTML = nstart + ' to ' + nend;
}
</script>

Now just replace the inputs '1970-06-08' and '1970-06-13' by some variables with a string of the date.

source (with modification): http://www.w3schools.com/jsref/jsref_getutcday.asp

Hope it helps;)

EBH
  • 10,350
  • 3
  • 34
  • 59
  • I'm trying to work out a range from the time values(start - end) which are given to me, how would I get that to work with this? –  Jul 12 '16 at 10:57
  • @edward I have edited the answer to return the day range – EBH Jul 12 '16 at 11:55
  • It does help, thanks heaps. Any idea how I might also get the local time for a specific region? for example: AEST, GMT, PT –  Jul 12 '16 at 12:23
  • 1
    You can try to use [`getTimezoneOffset()`](http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp), and also have a look [here](http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) and see if you find your answer. Otherwise, search a little bit more in **SO** and [google](https://www.google.co.il/webhp?sourceid=chrome-instant&rlz=1C1CHVZ_iwIL680IL681&ion=1&espv=2&ie=UTF-8#q=get%20the%20local%20time%20for%20a%20specific%20region%20in%20javascript) (since probably someone already wrote a function for that), and if you don't find, start another question. – EBH Jul 12 '16 at 12:50
  • Thanks heaps for all the help –  Jul 12 '16 at 12:53
  • Hey EBH, I have noticed that the start day is off by one. Any idea how I can fix this? –  Jul 12 '16 at 18:24
  • This is how `getUTCDay()` return it: "A Number, from 0 to 6, representing the day of the week". – EBH Jul 12 '16 at 18:30
  • I understand however, it currently showings the work week being Sun-Fri when it should be Mon-Fri. –  Jul 12 '16 at 23:05