Open live version
Find Your Age in Days
Find out how many days old you are, and how many days until your next birthday.
code
DateDifference["Jan 4, 1987", Today]
DateDifference[Today, "Jan 4, 2015"]
Make a website that shows days old and days until next birthday:
DaysOld[birthdate_] :=
DateDifference[birthdate, Today]
DaysToNextBirthday[birthdate_] :=
Block[{year, nextBirthday},
(* get the current year *)
year = Today["Year"];
(* replace the birthday year with the year of the next birthday *)
nextBirthday = DateList[birthdate];
nextBirthday[[1]] = year;
If[DateDifference[Today, nextBirthday] < 0,
nextBirthday[[1]] = year + 1];
(* return the number of days to the next birthday *)
DateDifference[Today, nextBirthday]
]
CloudDeploy[FormFunction[
FormObject[{"date" -> <|"Interpreter" -> "Date",
"Label" -> "birth date:"|>}],
Style[Row[{" You are ",
Style[DaysOld[#date], 18], " days old and have ",
Style[DaysToNextBirthday[#date], 18],
" days until your next birthday."}],
FontFamily -> "Times"] &,
"HTML"], Permissions -> "Public"]
how it works
This example was live-coded at the Wolfram Research booth at SXSW, March 9, 2014.
How many days old are you?
DateDifference["Jan 4, 1987", Today]
How many days until your next birthday?
DateDifference[Today, "Jan 4, 2015"]
You can package those questions as functions. DaysOld is trivial:
DaysOld[birthdate_] :=
DateDifference[birthdate, Today]
DaysToNextBirthday requires a little fancy footwork to construct the date of the next birthday:
DaysToNextBirthday[birthdate_] :=
Block[{year, nextBirthday},
(* get the current year *)
year = Today["Year"];
(* replace the birthday year with the year of the next birthday *)
nextBirthday = DateList[birthdate];
nextBirthday[[1]] = year;
If[DateDifference[Today, nextBirthday] < 0,
nextBirthday[[1]] = year + 1];
(* return the number of days to the next birthday *)
DateDifference[Today, nextBirthday]
]
Make a web page using those functions. Use Style and Row to format the output nicely:
CloudDeploy[FormFunction[
FormObject[{"date" -> <|"Interpreter" -> "Date",
"Label" -> "birth date:"|>}],
Style[Row[{" You are ",
Style[DaysOld[#date], 18], " days old and have ",
Style[DaysToNextBirthday[#date], 18],
" days until your next birthday."}],
FontFamily -> "Times"] &,
"HTML"], Permissions -> "Public"]
Click the URL in the previous output to visit the page.