-2

The tm_wday (e.g., weekday) property on the C++ time structure struct tm represents days since Sunday, an integer from 0-6. This is often used in daylight savings calculations; e.g., "The first Sunday in November."
http://www.cplusplus.com/reference/ctime/tm/

How is the equivalent calculated in JavaScript? In other words, how can "Days since Sunday" be found using a JavaScript Date() object?

I Googled around, and checked the MDN docs, but there does not appear to be an equivalent method.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Is anyone aware of a polyfill or get_days_since_sunday() method that has been written? Is there an easy way to do this, or do I need to write it? Thx, Keith :^)

user0042
  • 7,917
  • 3
  • 24
  • 39
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • 1
    Note that if you're going to use this for any "Nth day of the week" purposes, know that not everyone starts their week on Sunday. – chris Nov 07 '17 at 20:36
  • As Tom scott said in the [Computerphile video about timezone](https://www.youtube.com/watch?v=-5wpm-gesOY), you should always find a library that does that. i suggest momentJS function `moment().day()` that returns exaclty that. – Nicolas Nov 07 '17 at 20:41

2 Answers2

2

There is a simple way to find number of days between today and last sunday. Feel free to ask me if you have any question.

var date = new Date();
date.getDay();
Sadek Lallouani
  • 201
  • 1
  • 4
1

You can use Date#getDay to return the zero-based index of the weekday, starting with Sunday:

new Date.getDay() // returns 2, if today is a Tuesday
djfdev
  • 5,747
  • 3
  • 19
  • 38