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())
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:
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 do I format a date in JavaScript?
(68 answers)
Closed 5 years ago.
Consider:
var myDate = $scope.today;
var previousDay = new Date(myDate);
previousDay.setDate(myDate.getDate() - 1);
var date = previousDay; // 2017-03-04 09:46:22.103 +5.30 GMT
How do I get to only have "2017-03-03 09:46:22.103"?
You can use
var requiredDateFormat = previousDay.toLocaleDateString() + " " + previousDay.toLocaleTimeString();
You can use new Date().toJSON();. It will return "2017-03-04T04:55:50.786Z".
This question already has answers here:
JavaScript date objects UK dates
(6 answers)
Closed 9 years ago.
I have the below code. If we try to retrieve the day, month and year in UK server setup. The date object returns an incorrect value.
var startDate = "30/08/2013";
var d = new Date(startDate);
alert(d.getFullYear()); //2015
Please help me
Use moment.js if you want better control of parsing:
var startDate = "30/08/2013";
var m = moment(startDate,"DD/MM/YYYY");
alert(m.year()); //2013
Try passing the date as a list of arguments instead:
var startDate = "30/08/2013"; // Would also work with 30-08-2013
var startDateArray;
if(startDate.contains('/'))
startDateArray = startDate.split('/');
else if (startDate.contains('-'))
startDateArray = startDate.split('-');
else if (startDate.contains('.'))
startDateArray = startDate.split('.');
var d = new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]); // Month is 0 based, that's why we are subtracting 1