Javascript: check if timestamp is 24hrs old or less [duplicate] - javascript

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>

Related

Get age from date of birth with javascript and php [duplicate]

This question already has answers here:
javascript - Age calculation
(14 answers)
Closed 7 months ago.
I am trying through a date that in js returns me the years and months that have passed, but I only get it to show me the days
$("#nacimiento").change(function(){
var nacimiento=$("#nacimiento").val();
var fechaInicio = new Date(nacimiento).getTime();
var fechaFin = new Date('<? echo date("Y-m-d");?>').getTime();
var diff = fechaFin - fechaInicio;
var tiempo=diff/(1000*60*60*24);
console.log(diff/(1000*60*60*24));
document.getElementById('meses').innerHTML=tiempo;
});
You could use the Luxon package for this and create two DateTime objects and get the difference between them in Months from the Interval object and work from there.
eg
const fromUser = DateTime.fromString(nacimiento)
const fromPhp = DateTime.fromString('<? echo date("Y-m-d");?>')
const diff = Interval.fromDateTimes(fromUser, fromPhp);
const diffMonths = diff.length('months')
console.log(diffMonths)
another approach without an external package might be just using getMonth() and getFullYear() example:
const fromUser = new Date(nacimiento)
cnost fromPhp = new Date('<? echo date("Y-m-d");?>')
const months = fromUser.getMonth() -
fromPhp.getMonth() +
12 * (fromUser.getFullYear() - fromPhp.getFullYear())

Why setDate() is giving the final date in milliseconds in nodejs? [duplicate]

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());

Javascript logic to find the date based on the given number that is before or after excluding weekends [duplicate]

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

invalide date after converting epoch time vue.js [duplicate]

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/

How to compare 2 times in 24 hour format in javascript [duplicate]

This question already has answers here:
Compare two dates with JavaScript
(44 answers)
Compare two time (hh:mm:ss) strings
(8 answers)
Closed 5 years ago.
What is the best wayto compare 2 'time' in 24 hour format in javascript?
I have converted the time to 24 hour format.
For eg:
t1, t2; //These are 2 time in 24 hour format of **string** type
//Like t1="19:32" and t2 = "02:09"
if(t1<t2){} // Will this directly work for all times?
Any leads are appreciated.
One way you could do this, if the time is a string and always in the HH:MM 24 hour format, is parse the string and convert it into a number of minutes before performing the comparison. For example:
var time1 = "10:30";
var time2 = "12:30";
var time1InMinutesForTime1 = getTimeAsNumberOfMinutes(time1);
var time1InMinutesForTime2 = getTimeAsNumberOfMinutes(time2);
var time1IsBeforeTime2 = time1InMinutesForTime1 < time1InMinutesForTime2;
function getTimeAsNumberOfMinutes(time)
{
var timeParts = time.split(":");
var timeInMinutes = (timeParts[0] * 60) + timeParts[1];
return timeInMinutes;
}
In this example, time1 will work out at 60,030 minutes and time2 will work out at 72,030 minutes, turning the comparison into a check between two numbers.

Categories

Resources