Thư viện Javascript tính toán thời gian mặt trời mọc/lặn và hơn thế nữa
Cloned từ NOAA Solar Calculator NOAA
- Tính toán giờ mặt trời mọc/lặn
- Cung cấp các hàm tính toán thiên văn như
getGeomMeanLongSun,getEccentEarthOrbit,...
Cài đặt qua NPM
npm install @nghiavuive/sunset_calcKhi cài đặt xong, ta có thể import bằng import hoặc require. Trước khi bắt đầu, đảm bảo rằng package.json có "type": "module".
import { LunarDate, SolarDate } from "@nghiavuive/sunset_calc";Nếu sử dụng Typescript, lưu ý cấu hình tsconfig.json như sau:
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node",
"module": "ESNext"
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}Nếu sử dụng require
const calendar = require("@nghiavuive/sunset_calc/dist/index.cjs");Sử dụng qua jsDelivr
<script src="https://cdn.jsdelivr.net/gh/NghiaCaNgao/SunCalc@latest/dist/index.umd.js"></script>Sử dụng ES Module với Typescript. JavaScript tương tự.
Note Nếu sử dụng
ts-nodethì cần chạynpx ts-node --esm <filename>
Code bên dưới minh họa sử dụng Sun class để tính thời điểm mặt trời lặn.
import { ISun, Sun } from "@nghiavuive/sunset_calc";
const options: ISun = {
date: new Date(),
lat: 21,
long: 105,
time_zone: 7,
};
const sun = new Sun(options);
console.log(sun.getSunsetTime());
//18:44:24Nếu sử dụng CommonJs
const _sun = require("@nghiavuive/sunset_calc/dist/index.cjs");
const options = {
date: new Date(),
lat: 21,
long: 105,
time_zone: 7,
};
const sun = new _sun.Sun(options);
console.log(sun.getSunsetTime());
//18:44:24Input của Sun class
interface ISun {
lat: number; // + to N
long: number; // + to E
time_zone: number; // + to E
date: Date;
}Note Ngày nhập vào phải bắt đầu từ 1970-01-01
constructor(props: ISun);Ví dụ:
import { ISun, Sun } from "@nghiavuive/sunset_calc";
const options: ISun = {
date: new Date(),
lat: 21,
long: 105,
time_zone: 7,
};
const sun = new Sun(options);Đặt lại ngày cho thực thể Sun
setDate(date: Date): voidĐặt lại vịt trí cho thực thể Sun
setLatLong(lat: number, long: number):voidĐặt lại múi giờ cho thực thể Sun
setTimeZone(time_zone: number): voidLấy thông tin của thực thể Sun
get();Ví dụ:
import { ISun, Sun } from "@nghiavuive/sunset_calc";
const options: ISun = {
date: new Date(),
lat: 21,
long: 105,
time_zone: 7,
};
const sun = new Sun(options);
sun.setDate(new Date(2020, 1, 1));
sun.setLatLong(21, 105);
sun.setTimeZone(7);
console.log(sun.get());
// {
// lat: 21,
// long: 105,
// time_zone: 7,
// date: 2020-01-31T17:00:00.000Z,
// jd: 2458880.2083333335
// }Lấy thời gian mặt trời lặn theo định dạng hh:mm:ss
getSunsetTime(): stringLấy thời gian mặt trời mọc theo định dạng hh:mm:ss
getSunriseTime(): stringLấy thời gian mặt trời chiếu sáng theo định dạng hh:mm:ss
getSunlightDuration(): stringVí dụ:
import { ISun, Sun } from "@nghiavuive/sunset_calc";
const options: ISun = {
date: new Date(2023, 5, 23),
lat: 21,
long: 105,
time_zone: 7,
};
const sun = new Sun(options);
console.log(sun.);
// 13:24:31
// 05:19:46
// 18:44:17convertToJulianDay, getApproxAtmosphericRefraction, getEccentEarthOrbit,
getEqOfTime, getGeomMeanAnomSun, getGeomMeanLongSun, getHASunrise, getHourAngle,
getJulianCentury, getMeanObliqEcliptic, getObliqCorr, getSolarAzimuthAngle,
getSolarElevationAngle, getSolarElevationCorrectedForAtmRefraction, getSolarNoon,
getSolarZenithAngle, getSunAppLong, getSunDeclin, getSunEqOfCtr, getSunlightDuration,
getSunRadVector, getSunriseTime, getSunRtAscen, getSunsetTime, getSunTrueAnom,
getSunTrueLong, getTrueSolarTime, getVarY
deg2rad, excelMod, formatTime, rad2deg
Explain soon