Skip to content

Instantly share code, notes, and snippets.

@AcheZeta
Created September 10, 2019 20:21
Show Gist options
  • Save AcheZeta/4f4c550ca261f3e6dd296d761aa5aedc to your computer and use it in GitHub Desktop.
Save AcheZeta/4f4c550ca261f3e6dd296d761aa5aedc to your computer and use it in GitHub Desktop.
Get Number of Day In Year
//Const from date.
const pokebithday = document.getElementById("date-pick");
//Function to convert date into day Number.
const getUserId = () => {
//Get the Value from the input
let userBirthday = pokebithday.value
// use Date.parse() to convert a string representation of a date, and returns the number of milliseconds.
let parseDate = Date.parse(userBirthday)
//Slice the year from the input and set the fist day of Year.
let startOfYear = userBirthday.slice(0, 4) + '-01-01'
//Parse the Fisrt day of Year with Date.parse()
let parseStart = Date.parse(startOfYear)
//Get the number of day convert the Milliseconds into Days using this formula: 1000 * 60 * 60 * 24
//I use 86400000 which are the seconds in 24 hours
let days = 86400000
//Get the difference between the first day of the year and the select date.
let diff = parseDate - parseStart
//Round up the number obtained from the division and add one
let numberOfDate = Math.ceil(diff / days) + 1
//Set the UserId as the result
userIdPokemon = numberOfDate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment