This question already has answers here:
Show week number with Javascript?
(15 answers)
Closed 6 years ago.
All:
I wonder if there is a function in javascript that can get the week order of a Date, for example:
01/05/2016 is in the second week of this year, so the week order is 1(let start by 0)
Thanks
You can calculate the days pass from the begining of the year and calculate the number of weeks by deviding it to 7 (yhe number of the days in a week):
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
var week = Math.floor(day/7);
alert(week);
Related
This question already has answers here:
How can I calculate the number of years between two dates?
(22 answers)
Closed 2 years ago.
My code:
function test(val) {
year = parseInt(val.slice(0,2)); // get year
month = parseInt(val.slice(2,4)); // get month
date = val.slice(4,6); // get date
if (month > 40) { // For people born after 2000, 40 is added to the month. (it is specific for my case)
year += 2000;
month -= 40;
} else {
year += 1900;
}
date = new Date(year, month-1, date, 0, 0);
date_now = new Date();
var diff =(date_now.getTime() - date.getTime()) / 1000;
diff /= (60 * 60 * 24);
console.log(Math.abs(Math.round(diff/365.25)));
}
Example 1:
If I was born in
1993-year;
04-month(april);
26-date
I will pass 930426 as value to test function and the result would be 27 which is correct
But in Example 2:
If I was born in:
1993-year;
09-month(september);
14-date;
I will pass 930914 as value to test function and result would be 27, but it's not correct because my birthday is still not come and i'm still 26 years old.
How can I fix this ?
Because 26.9 is still regarded as age of 26, so you should use .floor instead
function test(val) {
year = +val.slice(0, 2) // get year
month = val.slice(2, 4) // get month
date = val.slice(4, 6) // get date
date = new Date(year, month - 1, date, 0, 0)
date_now = new Date()
var diff = (date_now.getTime() - date.getTime()) / 1000
diff /= 60 * 60 * 24
console.log(diff / 365.25)
console.log("Age", Math.floor(diff / 365.25))
}
test("930426")
test("930914")
This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Closed 4 years ago.
I need the difference between two dates in javascript, this is my date format 24-05-2018, and I need no of year, no of months, no of days
Use moment.js
It is much easy with this.
var date1 = moment('24-05-2018','DD-MM-YYYY');
var date2 = moment('24-05-2019','DD-MM-YYYY');
var years = date2.diff(date1, 'year');
date1.add(years, 'years');
var months = date2.diff(date1, 'months');
date1.add(months, 'months');
var days = date2.diff(date1, 'days');
date1 = new Date(date1.getUTCFullYear(), date1.getUTCMonth(), date1.getUTCDate(), date1.getUTCHours(), date1.getUTCMinutes(), date1.getUTCSeconds());
date2 = new Date(date2.getUTCFullYear(), date2.getUTCMonth(), date2.getUTCDate(), date2.getUTCHours(), date2.getUTCMinutes(), date2.getUTCSeconds());
let diff_ms = date2.getTime() - date1.getTime();
let diff = Math.round(diff_ms / (1000 * 60 * 60 * 24));
This will give you difference in no of days. You can then deduce year, month, days.
This question already has answers here:
how can I convert day of year to date in javascript?
(9 answers)
Closed 4 years ago.
From this question I get a very useful function, which can calculate the day of the year from a date.
My question is that how can I reverse it?
So when I pass a number to the function's parameter, which is the day of the year, the function returns a date?
Here's the code:
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);
Here is a function if you pass the year and the nth day of that year this will return the date of that nth day.
function dateFromDay(year, day){
var date = new Date(year, 0);
return new Date(date.setDate(day));
}
console.log(dateFromDay(2010, 301));
console.log(dateFromDay(2010, 365));
I think that this question should answer your problem.
Simply instantiate a new Date at the beginning of the year - January 1st - and add the number of days to get to the x-th one.
Hope it helps.
Add this after your code. There is the working fiddle https://jsfiddle.net/andreitodorut/ubLxy174/
var yearBegining = new Date();
yearBegining.setMonth(0,0);
yearBegining.setTime((yearBegining.getTime()/1000 + (86000 * day)) * 1000);
You can check the results with this content https://www.epochconverter.com/days/2018
This question already has answers here:
Get number days in a specified month using JavaScript? [duplicate]
(4 answers)
Closed 8 years ago.
I have a string that represents a date in this format: 2015-02-23
I need to use this date to get the last day of the month.
How should I do the necessary conversions to achieve that?
Here's a function that should work for you:
function getLastDayInMonth(s) {
var date = new Date(s);
var lastDate = date;
var month = date.getMonth();
while (date.getMonth() == month) {
lastDate = date;
date = new Date(lastDate.getTime() + 1000 * 60 * 60 * 24); //add 1 day
}
return lastDate.toDateString();
}
var lastDay = getLastDayInMonth('2015-02-23');
alert(lastDay);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to calculate the number of days between two dates using javascript
I have those dates :
27/09/2011
29/10/2011
and I'd like to return the days between those dates (in the example, should be 33 days).
How can I do it on javascript (or jquery?)?
var daysBetween = (Date.parse(DATE1) - Date.parse(DATE2)) / (24 * 3600 * 1000);
function days_between(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}
http://www.mcfedries.com/JavaScript/DaysBetween.asp
// split the date into days, months, years array
var x = "27/09/2011".split('/')
var y = "29/10/2011".split('/')
// create date objects using year, month, day
var a = new Date(x[2],x[1],x[0])
var b = new Date(y[2],y[1],y[0])
// calculate difference between dayes
var c = ( b - a )
// convert from milliseconds to days
// multiply milliseconds * seconds * minutes * hours
var d = c / (1000 * 60 * 60 * 24)
// show what you got
alert( d )
Note:
I find this method safer than Date.parse() as you explicitly specify the date format being input (by splitting into year, month, day in the beginning). This is important to avoid ambiguity when 03/04/2008 could be 3rd of April, 2008 or 4th of March, 2008 depending what country your dates are coming from.