This question already has answers here:
How to add number of weeks to the current date using JavaScript?
(5 answers)
Closed 5 years ago.
1- I have a number of weeks from an input field:
$("#numberWeek").on("keyup", function(){
var numberWeek = $(this).val();
console.log(numberWeek);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="numberWeek" />
2- I have the number of the current week:
var todayDate = new Date();
var currentWeek = (0 | todayDate.getDate() / 7) + 1;
console.log(currentWeek);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
3- How can I display the date that we will find when we start to count from the current week (currentWeek) to the number of weeks that is in the variable numberWeek in this format : 26/07/2018 ?
Thanks in advance for any help.
You can use the moment.js (here documentation) library to achieve this easily, just use add method and pass as parameter week, something like this:
moment(todayDate).add(numberWeek, 'weeks').calendar()
here the idea working: https://jsbin.com/rokiluxosi/1/edit?html,js,output
Related
This question already has answers here:
setDate() returns number 1603240915215 instead of a date
(2 answers)
Closed 2 years ago.
I a trying to save a date to the nextMonth. For that I am first setting the month to next 30 days. But the final output date it is giving me in milliseconds.
I want the date in GMT format strictly.
What can I do for that?
var snm = new Date();
snm = snm.setDate(snm.getDate() + 30);
console.log("snm = "+ snm);
Try this
var snm = new Date();
snm.setDate(snm.getDate() + 30)
console.log("snm = "+ snm.toString());
This question already has answers here:
Calculating the difference between two dates
(10 answers)
Check if a date is 24 hours old
(5 answers)
What's the best way to calculate date difference in Javascript
(2 answers)
Closed 2 years ago.
I need to fetch bitbucket repositories and filter them according to the updated_on timestamp.
GOAL: Is to filter the ones which are updated in last 24 hours.
This is what I get from API : updated_on: 2018-01-30T11:45:32.902996+00:00
My code logic:
// repo's updated_on timestamp (example)
const time = '2018-01-30T11:45:32.902996+00:00'
let currentTime = Date.now()
let updatedOn = new Date(time).getTime()
const filter = () => {
// boolean
return (currentTime - updatedOn) > 86400000)
}
if (filter()) {
// NOT FILTERING // giving out last month's data as well
console.log(updatedOn)
}
Ciao, with moment library you could use isBefore function in this way:
let date = "2018-01-30T11:45:32.902996+00:00";
let anotherDate = moment(); // today
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(date))));
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(anotherDate))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
This question already has answers here:
add/subtract business days in Javascript
(6 answers)
Closed 2 years ago.
I have a strange requirement where I need a find the date based on given number and type = before/after parameter
i.e., for example
Date = new Date() - 3/8/2020 (It can be any date)
type = before
days = 7(It can be any number)
Now I need to find the date excluding the weekends.
In this case it would be 23/7/2020 .Because 2/8,1/8,26/7,25/7 are weekends, so those should be excluded and calculated.
Simillarly if type = after , date will be 12/8/2020 .In this case 8/8 and 9/8 are weekends and those will be excluded.
So how can we implement this as function that takes date,days,type as parameters and return the date.
i.e.,
function calculateDate(day,days,type){
/* Some logic
if(new Date(day).getDay() !== 6||7){};
*/
return date
}
Note : I cant use any libraries like moment/dayjs etc as I have restrictions with the development tool Im using..I need to implement in pure javascript logic
Can any javascript datetime expert help me on this as I couldn't crack this.
Thanks in advance
const date = new Date()
let days = 7 // Adjust accordingly
let delta = -1 // Set +1 to check 'days after' instead of 'days before'
while (days > 0) {
// Remove a day
date = date.setDate(date.getDate() + delta)
// Check if weekend (adjust your days number according to your needs)
const isWeekend = date.getDay() === 6 || date.getDay() === 0
// If it is not a weekend, reduce the number of days to subtract
if (!isWeekend) {
days = days - 1
}
}
return date
This question already has answers here:
Convert UNIX timestamp to date time (javascript)
(4 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
Im trying to make a table with vue.js and one of the row is supposed to print a date i receive in epoch time. but fail to convert it in a readable date and just get "invalide date" printed on my browser.
Here is how i convert it
var sec = 1588351494;
var tmp =new Date(0);
tmp.setUTCDate(sec);
var res= tmp;
her is how it's made to be able to be called as a vue.js object
return {
tableData: [
{
uid: '01020304050607',
lastReadDate: res,
},
]
}
And then i simply print it in my html page doing this, which print the "invalide date" in the row.
<td>
{{row.lastReadDate }}
</td>
var sec = 1588351494;
var res =new Date(sec * 1000); // multiply seconds with 1000 to convert it to ms.
Check the fiddle: https://jsfiddle.net/vf74h5n0/4/
To format date in "DD/MM/YYYY" you can use momentjs.
var date = new Date(1588351494 * 1000);
moment(date).format("DD/MM/YYYY");
Here is a example https://jsfiddle.net/vf74h5n0/5/
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 6 years ago.
I've a form with fields .daysfromtoday and #datefromtoday.
I would like to enter a number of days in .daysfromtoday and update #datefromtoday with the date yyyy-mm-dd format. (I know that there are other posts about this subject, but, if possible, I would like someone helps me with code I've written, to learn more)
HTML:
<input type="text" class="daysfromtoday" />
<input type="text" id="datefromtoday" name="delay" />
javascript:
$(document).ready(function (){
$(".daysfromtoday").on('change', function(){//
var waitdays = $( ".daysfromtoday" ).val(); //take value from field .daysfromtoday
var enddate = new Date();
enddate.setDate(enddate.getDate() + waitdays);
var yyyy = enddate.getFullYear().toString();
var mm = (enddate.getMonth()+1).toString();
var dd = enddate.getDate().toString();
document.getElementById("datefromtoday").value = yyyy + '-' + mm + '-' + dd; //outuput
});
});
Problems: (and solutions)
I get weird results: today is 2016-02-26, if I enter 100, I get 2087-7-17
The result format should have the 0 (zeros): 2016-03-08
You probably add string instead of number to the date. Try using parseInt function:
...
enddate.setDate(enddate.getDate() + parseInt(waitdays, 10));
...