This question already has answers here:
Javascript code for showing yesterday's date and todays date
(6 answers)
Closed 6 years ago.
I want to plot a graph by fetching the previous seven days using the present day in Javascript.
Which should pass through the tests of Leap year, days of mont(30/31), Year change.
Thank you in advance.
var d = new Date();
var yesterday = d.setDate(d.getDate() - 1);
Simply replace - 1 with - 2, - 3 and so on for the previous seven days.
Related
This question already has answers here:
Get week of the month
(17 answers)
Closed 2 years ago.
I need to display a whole week from Monday To Sunday in following format "Week 2: 05.01 - 11.01" as an example
let d = new Date();
let date = d.getDate();
let day = d.getDay();
let currentWeek = Math.ceil((date + 6 - day) / 7);
this is what i have for the current week, i know that it will have some issues if the previous month is in the first week of the current month.
Any help as to how to make it accurate and reliable?
I need it to work in React Native!
I like using moment to deal with Dates, since date operations are somewhat complex considering month changes, leap years and all that.
Using moment, you'd just need to do
const today = moment();
const begginingOfCurrentWeek = today.startOf('week');
const endOfWeek = today.endOf('week);
I havent tested it, but it should be something along those lines. You can also format dates easily using moment().format('DD/MM/YY'), for example.
Here's the link to their documentation:
https://momentjs.com/
This question already has answers here:
JavaScript - get the first day of the week from current date
(20 answers)
Closed 4 years ago.
$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I got this JS code:
var d = new Date();
d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
document.write(d);
and I want JS to display it in dd/mm/yyyy. How can I do that?
Thank you,
Till
Try this link to W3 Schools date formatting:
https://www.w3schools.com/js/js_date_formats.asp
and date methods:
https://www.w3schools.com/js/js_date_methods.asp
and check out this previous S/O answer:
How to convert a full date to a short date in javascript?
This question already has answers here:
Date difference in Javascript (ignoring time of day)
(15 answers)
How to calculate number of day name between 2 dates in javascript
(1 answer)
Closed 6 years ago.
I am trying to calculate the number of days since a given date.
today = new Date();
time = today - mydate;
days = Math.floor(time / (1000*60*60*24));
This returns the correct value only if mydate is set to a time before the current time. In other words, if mydate is yesterday at 10 pm and I run this in the morning, I get 0 when I want 1.
This question already has answers here:
Difference in Months between two dates in JavaScript
(29 answers)
Closed 8 years ago.
I'm needing to write a function that takes two dates (startDate, endDate) calculates the time between them in months or years (depending on an input). When the number of months or years is found, I need to iterate a loop that runs a very simple ajax call (I'm adding a record to Quickbase) for every month OR year between the start and end dates.
I've taken a couple of hacks at this but I'm running into trouble when I try to calculate months (due to the calendar dates (i.e. feb having 28 days etc)... I've calculated the time between the two dates in days, but I need months or years.
Any help would be appreciated!
just to start try
function Noofmonths(date1, date2) {
var Nomonths;
Nomonths= (date2.getFullYear() - date1.getFullYear()) * 12;
Nomonths-= date1.getMonth() + 1;
Nomonths+= date2.getMonth() +1; // we should add + 1 to get correct month number
return Nomonths <= 0 ? 0 : Nomonths;
}
obtain date1 year and date2 year.
get the difference and multiply by 12.
obtain date1 month and date2 month.
get the difference.
add the differences together.