Calculating age in Javascript with YYYY-mm-dd format [duplicate] - javascript

This question already has answers here:
Calculate age given the birth date in the format YYYYMMDD
(45 answers)
Closed 6 years ago.
I have birthday in following format:
1982-09-20
I need to get the exact age of the person from that birthdate ( compared with the current date).
What's the easiest way to do this in JS? Can someone help me out please?
Thanks!

I assume you want the exact age in years. If you want another value, it's all in that magic number of have in the function below, a bit verbose for clarity:
var MILLISECONDS_IN_A_YEAR = 1000*60*60*24*365;
function get_age(time){
var date_array = time.split('-')
var years_elapsed = (new Date() - new Date(date_array[0],date_array[1],date_array[2]))/(MILLISECONDS_IN_A_YEAR);
return years_elapsed; }
You would call:
get_age ('1982-09-20')
This is probably going to be marked as a duplicate and deleted.
There might be a better in built way, but this should work fine for your needs.

Related

Getting the date of the nth day after a given date [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!

calculate whether weekend weekday or holiday [duplicate]

This question already has answers here:
Determine if a date is a Saturday or a Sunday using JavaScript
(6 answers)
Checking if date belongs to array of dates
(3 answers)
How to determine if date is weekend in JavaScript
(11 answers)
Moment JS Excluding Holidays
(2 answers)
Closed 3 years ago.
I have a start date and end date from my request need to find whether the given day is weekday or weekend or holiday(from list of holidays in db)
I tried using moment.js
moment.js is just a library to manage dates, what you need is an algorithm to find if one of the dates is weekend or weekday.
So what you can do is get the day number from the date (0-6) where 0 is sunday.
Let's say
var date = moment("2015-07-02");
var dow = date.day();
And about holidays or not or not, that's simple also, you need to get an array with all the dates in the same format that you are using with moment.js and iterate through the array to do it something like:
var holidays = ["2019-07-02", "2019-07-04"]
let isHoliday = holidays.find(x => x === "2015-07-02")
You need to do some work after all you need is here.
To determine if a given date is a weekend is actually pretty simple and already has an answer here.
To determine if a given date is holiday is bit more complex, you can not achieve it with native JavaScript, you will need the help of external package.
I recommend you to use moment-holiday, it has a simplest API:
moment('2017-12-25').isHoliday();
//Christmas Day
moment('2005-03-15').isHoliday();
//false
moment('2009-10-31').isHoliday('Halloween');
//true
moment('2017-12-31').isHoliday();
//New Year's Eve
moment('2017-12-31').isHoliday(null, true);
//false
Or you can use date-holidays.

Javascript Determine 10 days before specific date [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 4 years ago.
So I am currently fetching some records from my database, and one of the records is a 'last edited' which is a date. However, I just want to display this 10 days before it has been exactly one year since the specific date. How would I approach this? I know how to deal with arrays and the fetching itself, but I'm not sure how I would deal with the date?
This is what I've done so far:
var startDate = fetchedDate;
var validDate = new Date(startDate);
var fullYear = validDate.getFullYear();
validDate.setFullYear(fullYear + 1);
And then I need to create a new date to compare it, I suppose? But how? I also want to know how many days it is until it has been one year.
If you wanna make it with pure javascript you can
var startDate = fetchedDate;
var validDate = new Date(startDate);
validDate.setYear(validDate.getFullYear() - 1);
validDate.setDate(validDate.getDate() - 10);
But I also will recommend you to use moment.js, is a very good javascript library to handle dates, otherwise sometimes is like trying to reinvent the wheel.

Jquery/JEasyUI How do I get the current date? [duplicate]

This question already has answers here:
How do I get the current date in JavaScript?
(61 answers)
Closed 8 years ago.
What I need to do is get the current date and do a math formula to figure out how many days from a recorded date in a record. Is there a Jquery or javascript function to do this?
you can use this code to find number of days between two dates.
var d1=new Date(2014,06,15);
var d2=new Date(2014,06,10);
var timeInSec=d1-d2;
var days=timeInSec/(1000*60*60*24);

How should I instantiate this date in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Format Date in Javascript
I have this date:
var newDate = "/Date(1333609200000)/";
And what I need to do to it is:
var myDate = new Date(1333609200000);
I thought about just trimming off "/Date()/" and then getting just the number inside but I wasn't sure if that would be best here. Should I just simply regex the number out of that? Is there a better approach?
Notes: The date is being created by c#'s JavascriptSerializer and comes in the format newDate above. It cannot be changed in c# due to constraints.
You could do it like this:
var newDate = "/Date(1333609200000)/";
newDate= new Date(parseInt(newDate.match(/\d/g).join("")));

Categories