score:0
var start = new date('march 1, 2020');
var period = [];
for(var i=1; i<=60; i++){
if( i == 32 )
start = new date('april 1, 2020');
if( i <= 31 )
start.setdate(i);
else
start.setdate(i - 31);
var datestring = start.todatestring().split(' ');
period.push({
day: datestring[0],
date: datestring[2],
month: datestring[1],
year: datestring[3]
});
}
console.log( json.stringify(period) );
score:0
i'm assuming you have two dates as your range delimiters. if not, you can create them this way:
var startdate = new date('1 march 2020')
var enddate = new date('29 april 2020')
then, you have to increase the first date by one day until you reach the last date. to increase the first date by one day you can do this:
startdate.setdate(startdate.getdate() + 1)
you can get the day and the month from a date with date.getday()
and date.getmonth()
, but those methods will return numbers. to get the actual names you can do this:
var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
var startdateday = days[startdate.getday()]
var startdatemonth = months[startdate.getmonth()]
and then, you iterate:
var period = []
while (startdate <= lastdate) {
period.push({
day: days[startdate.getday()],
date: startdate.getdate(),
month: months[startdate.getmonth()],
year: startdate.getyear()
})
startdate.setdate(startdate.getdate() + 1)
}
and that's it. here's a fiddle with a working example:
var startdate = new date('1 march 2020')
var enddate = new date('29 april 2020')
var days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
var period = []
while (startdate <= enddate) {
period.push({
day: days[startdate.getday()],
date: startdate.getdate(),
month: months[startdate.getmonth()],
year: startdate.getfullyear()
})
startdate.setdate(startdate.getdate() + 1)
}
console.log(period)
score:1
in traditional way you can do it like this
var startdate = new date('2020-03-10');
var enddatedate = new date('2020-03-12');
var arr = [];
var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
while(startdate.gettime() !== enddatedate.gettime()) {
startdate.setdate(startdate.getdate() + 1)
arr.push({
day: days[startdate.getday()],
date: startdate.getdate(),
month: startdate.getmonth(),
year: startdate.getyear()
})
}
console.log(arr);
score:2
you can create array from date range follow https://stackoverflow.com/a/50398144/4964569
and get day in date follow https://stackoverflow.com/a/4822882/4964569
and use map
function to generate your required
var getdaysarray = function(s,e) {for(var a=[],d=new date(s);d<=e;d.setdate(d.getdate()+1)){ a.push(new date(d));}return a;};
var daterange = getdaysarray(new date('2020-03-10'), new date('2020-04-29'));
var days = ['sun','mon','tue','wed','thu','fri','sat'];
var result = daterange.map(function(elem){
var obj = {
day: days[elem.getday()],
date: elem.getdate(),
month: elem.getmonth(),
year: elem.getfullyear()
}
return obj;
});
console.log(result)
var getdaysarray = function(s,e) {for(var a=[],d=new date(s);d<=e;d.setdate(d.getdate()+1)){ a.push(new date(d));}return a;};
var daterange = getdaysarray(new date('2020-03-10'), new date('2020-04-29'));
var days = ['sun','mon','tue','wed','thu','fri','sat'];
var result = daterange.map(function(elem){
var obj = {
day: days[elem.getday()],
date: elem.getdate(),
month: elem.getmonth(),
year: elem.getfullyear()
}
return obj;
});
console.log(result)
Source: stackoverflow.com
Related Query
- How to turn a period of date into an array?
- How do I turn part of Redux store into an array
- How to Turn a Multiple Array Object into Query String Parameters in JavaScript
- How can I turn an array object into an object?
- How to turn an Array of objects into a nested object?
- How to turn object values into new array in JavaScript
- How to get data from API and turn it into array list for Google Charts
- how do i turn data from my api to array i cant use map function so i want to turn cart.cartItems into an array
- How can I turn this function into a class in which the state would be an array containing the grid items?
- How to parse ISO 8601 into date and time format using Moment js in Javascript?
- How can I insert into React's state array with setState?
- How to access Object array file into react component?
- How to push into an array of object using the spread operator to a specific element
- React Formik checkbox group does not turn into an array of invidual checked or unchecked elements
- How to convert 2021-07-14T00:00:00.000Z into a standard date in YYYY-MM-DD format
- How to get Current week from given array of objects date in javascript
- how to disable all the Sundays and array of specific days in ant design date picker
- React addons update - how to insert an object into an array of objects?
- How to use setState to splice into an array in the state?
- How to add a each element of an array into React state
- How do I pass a value from an array defined in state into a component which beholds a component?
- How to take a comma separated array and convert it into individual list items? React.js & Javascript
- How to push an object into an array with React Hooks
- how should we convert string data into array and display into react table
- How to add items into specific Id using the react array map method
- how would i aggregate array of objects into groups, dynamically
- how to map array of images into two images per list item React JS
- How to turn redux-thunk action creator into Promise?
- How to add object into array with ES6 ... method ?
- how to push objects into array in ReactJS
More Query from same tag
- reactjs setInterval doesn't work as expected
- Styled-components with animate.css
- Passing function/method down in my route file react
- Why is async await very slow?
- Do I have to re-list all my React Font Awesome icons from import and adding to library?
- Bind click to a div and get attribute data in React
- Why in shouldComponentUpdate() nextProps and this.props are the same?
- Error on running npm run dev
- API confirmation of success other than using response status
- Send data react axios
- react node server request port change
- I have a issue with a counter of steps - reactjs form
- How to change state of button on click and revert back to the original state when clicked again in React?
- Accessing a property that is also an Array {React, Javascript} returns undefined
- UseContext doesn't re-execute component
- Problems with installing SASS React
- NextJS - Routing with different language
- How to extends classes in material UI createStyles
- React testing with Enzyme, componentDidMount updating HTML()
- javascript / react - switching the properties of two objects in an array in react state without triggering a re-render
- Multiple react-router-relay routes using same Relay root query with different fragment fields
- React - Remove image based on position within window
- Having trouble importing Image into React App
- Should you ever use this.setState() when using redux?
- Children stored in an array in Parent's state don't receive new props
- Integrating two react functions in a synchronous way
- How to integrate Script tag into a body tag at end in an SPFX
- How to declare this.someProperty in react with typeScript?
- Mapping Through an Array w/ React and Bootstrap
- React hooks pagination with useEffect - how to revert page number if request fails?