javascript - How to check if it's somebody's birthday in JS (with time zone)?
问题描述
Earlier I asked about the concepts of storing birth dates and notifying users, after looking at some libraries I decided to go for date-fns and date-fns-tz, I figured I can store dates like this:
import { utcToZonedTime, zonedTimeToUtc } from "date-fns-tz";
// Picked by user
const timeZone = "America/Los_Angeles";
const birthDate = "03-11";
const utcDate = zonedTimeToUtc(`2000-${birthDate} 00:00:00`, timeZone);
// Stored in database
console.log(utcDate.toISOString(), timeZone);
I am still missing the concept because I am stuck at the part where I check if it's somebody's birthday regardless of where my server is hosted, let's say I run a cron job at 0 0 * * *
, how would I know which people to message? I accept either a postgres or JavaScript solution (or combined)
解决方案
We can make the PostgreSQL query like this
SELECT * FROM TABLE_NAME
WHERE
DATE_PART('day', DOB) = date_part('day', CURRENT_DATE)
AND
DATE_PART('month', DOB) = date_part('month', CURRENT_DATE)
AND
cast(DOB as time) >= localtime;
where DOB is the birthdate stored.
Another solution which will acount for leap years as well
SELECT * FROM TABLE_NAME
WHERE
EXTRACT(YEARS FROM age(CURRENT_TIMESTAMP, DOB)) > user_age;
age is an inbuilt function which takes cares of the leap years
as well,
we can store the user_age of the user and update when it changes.
推荐阅读
- r - 使用 ggplot scale_x_yearmon 缺少 yearmon 标签
- sql - SSIS 中的 Google Analytics/Adwords 数据?
- dataframe - 为什么 Dask 在我的 Dataframe 中填写“foo”和 1
- javascript - 如果您只知道属性名称,您能否通过 setter 和 getter 从对象的列表中定义属性?
- python - 如何让3个线程依次打印
- c++ - 上限 time_point 到运行时定义的持续时间
- python - 使用 xml.dom.minidom 在 Python 中进行 XML 注入
- php - 过滤整个 $_POST 变量 - 这会安全吗?
- sql - 如何修复 OLEDB、SQL 中的更新和设置命令?
- amazon-web-services - 没有得到詹金斯解锁路径