Get the day number from moment object javascript

Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.
What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?
var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];
days = sum - (month_day[mon-1] - day);
console.log(days)
javascript momentjs
add a comment |Â
up vote
6
down vote
favorite
I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.
What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?
var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];
days = sum - (month_day[mon-1] - day);
console.log(days)
javascript momentjs
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
3
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.
What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?
var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];
days = sum - (month_day[mon-1] - day);
console.log(days)
javascript momentjs
I have a moment data object, what i want to do is get the date number, like if 2018-12-31 is given, it should return 365.
What I've currently done is this, but I feel like this is a more brute force approach since I have to run this function over and over again. Is there a more elegant way of doing this through the momentjs library?
var day = 25;
var mon = 12;
var year = 2018;
var sum = 0;
var days = 0;
var month_day = [31,28,31,30,31,30,31,31,30,31,30,31];
for ( var i = 0; i < mon; i++)
sum += month_day[i];
days = sum - (month_day[mon-1] - day);
console.log(days)
javascript momentjs
javascript momentjs
edited Sep 10 at 9:45
Sumurai8
12.2k73059
12.2k73059
asked Sep 10 at 3:50
Nimesha Kalinga
1058
1058
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
3
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52
add a comment |Â
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
3
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
3
3
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
13
down vote
accepted
You can use the dayOfYear() function:
const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>add a comment |Â
up vote
3
down vote
You can do it without momentjs
let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
add a comment |Â
up vote
0
down vote
https://momentjs.com/docs/#/get-set/day-of-year/
var day = 25;
var mon = 12;
var year = 2018;
console.log(moment().year(year).month(mon).date(day).dayOfYear());
You should check the docs before asking questions on Stack Overflow.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
You can use the dayOfYear() function:
const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>add a comment |Â
up vote
13
down vote
accepted
You can use the dayOfYear() function:
const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>add a comment |Â
up vote
13
down vote
accepted
up vote
13
down vote
accepted
You can use the dayOfYear() function:
const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>You can use the dayOfYear() function:
const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>const day = 25;
const month = 12 - 1; // months are 0-based when using the object constructor
const year = 2018;
const date = moment(day, month, year);
console.log(date.dayOfYear()); // 359<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>edited Sep 10 at 4:03
VicJordan
10.1k74069
10.1k74069
answered Sep 10 at 4:00
Robby Cornelissen
40.1k126488
40.1k126488
add a comment |Â
add a comment |Â
up vote
3
down vote
You can do it without momentjs
let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
add a comment |Â
up vote
3
down vote
You can do it without momentjs
let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can do it without momentjs
let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);You can do it without momentjs
let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);let year = 2018;
let month = 12 - 1;
let day = 25;
let dayOfYear = (Date.UTC(year, month, day) - Date.UTC(year, 0, 1)) / 86400000 + 1;
console.log(dayOfYear);edited Sep 10 at 4:35
answered Sep 10 at 4:08
Dmitry Kolchev
77435
77435
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
add a comment |Â
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
1
1
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
Info: momentjs use a somewhat similar approach.
â user202729
Sep 10 at 11:30
add a comment |Â
up vote
0
down vote
https://momentjs.com/docs/#/get-set/day-of-year/
var day = 25;
var mon = 12;
var year = 2018;
console.log(moment().year(year).month(mon).date(day).dayOfYear());
You should check the docs before asking questions on Stack Overflow.
add a comment |Â
up vote
0
down vote
https://momentjs.com/docs/#/get-set/day-of-year/
var day = 25;
var mon = 12;
var year = 2018;
console.log(moment().year(year).month(mon).date(day).dayOfYear());
You should check the docs before asking questions on Stack Overflow.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
https://momentjs.com/docs/#/get-set/day-of-year/
var day = 25;
var mon = 12;
var year = 2018;
console.log(moment().year(year).month(mon).date(day).dayOfYear());
You should check the docs before asking questions on Stack Overflow.
https://momentjs.com/docs/#/get-set/day-of-year/
var day = 25;
var mon = 12;
var year = 2018;
console.log(moment().year(year).month(mon).date(day).dayOfYear());
You should check the docs before asking questions on Stack Overflow.
answered Sep 10 at 3:58
DrevanTonder
324617
324617
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52250848%2fget-the-day-number-from-moment-object-javascript%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I know this is asked for php, but i want to know is, whether is there a elegant way of doing this using moment
â Nimesha Kalinga
Sep 10 at 3:51
3
Not only brute force, but also incorrect because it ignores leap years.
â Robby Cornelissen
Sep 10 at 3:52