Skip to content

Commit

Permalink
add fromModernJulianDate()
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Aug 1, 2024
1 parent da02c5c commit 38cbfae
Show file tree
Hide file tree
Showing 13 changed files with 548 additions and 302 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _config.yml
.gitignore
.npmignore
.prettierignore
.prettierrc.json

eslint.config.*
prettier.config.*
tsconfig.json
7 changes: 0 additions & 7 deletions .prettierrc.json

This file was deleted.

13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ npm install @cityssm/modern-julian-date

```javascript
import {
fromModernJulianDate,
toModernJulianDate,
toShortModernJulianDate
} from "@cityssm/modern-julian-date";

/*
* toModernJulianDate()
* returns date strings in YYYYDDD format.
* Returns date strings in YYYYDDD format.
*/

console.log( toModernJulianDate(new Date(2022, 1 - 1, 1)) );
Expand All @@ -38,9 +39,17 @@ console.log( toModernJulianDate(new Date(2020, 12 - 1, 31)) );

/*
* toShortModernJulianDate()
* returns date strings in YYDDD format.
* Returns date strings in YYDDD format.
*/

console.log( toShortModernJulianDate(new Date(2022, 1 - 1, 1)) );
// => '22001'

/*
* fromModernJulianDate()
* Returns Date objects.
*/

console.log( fromModernJulianDate('2020366') )
// => 'Thu Dec 31 2020 00:00:00 GMT-0500 (Eastern Standard Time)'
```
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export declare function getDayOfYear(date: Date, startAtZero?: boolean): number;
export declare function toModernJulianDate(date: Date): string;
export declare function toShortModernJulianDate(date: Date): string;
export declare function fromModernJulianDate(modernJulianDate: string): Date;
export default toModernJulianDate;
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ export function toModernJulianDate(date) {
export function toShortModernJulianDate(date) {
return toModernJulianDate(date).slice(2);
}
export function fromModernJulianDate(modernJulianDate) {
let sevenDigitModernJulianDate = modernJulianDate;
if (modernJulianDate.length === 5) {
sevenDigitModernJulianDate =
Math.floor(new Date().getFullYear() / 100).toString() + modernJulianDate;
}
if (sevenDigitModernJulianDate.length !== 7) {
throw new Error(`Invalid modern Julian date: ${modernJulianDate}`);
}
const year = Number.parseInt(sevenDigitModernJulianDate.slice(0, 4));
const days = Number.parseInt(sevenDigitModernJulianDate.slice(4));
if (Number.isNaN(year) || Number.isNaN(days) || days > 366) {
throw new TypeError(`Invalid modern Julian date: ${modernJulianDate}`);
}
const date = new Date(year, 1 - 1, 1);
date.setDate(days);
return date;
}
export default toModernJulianDate;
33 changes: 33 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,37 @@ export function toShortModernJulianDate(date: Date): string {
return toModernJulianDate(date).slice(2)
}

/**
* Converts a modern Julian date into a Javascript Date object.
* ex. '2023001' => 2023-01-01
* @param modernJulianDate - A five-digit (short) or seven-digit modern Julian date. Note that five-digit dates will be assumed to be in the current century.
* @returns Date object.
*/
export function fromModernJulianDate(modernJulianDate: string): Date {
let sevenDigitModernJulianDate = modernJulianDate

if (modernJulianDate.length === 5) {
sevenDigitModernJulianDate =
Math.floor(new Date().getFullYear() / 100).toString() + modernJulianDate
}

if (sevenDigitModernJulianDate.length !== 7) {
throw new Error(`Invalid modern Julian date: ${modernJulianDate}`)
}

const year = Number.parseInt(sevenDigitModernJulianDate.slice(0, 4))
const days = Number.parseInt(sevenDigitModernJulianDate.slice(4))

if (Number.isNaN(year) || Number.isNaN(days) || days > 366) {
throw new TypeError(`Invalid modern Julian date: ${modernJulianDate}`)
}

// eslint-disable-next-line sonarjs/no-identical-expressions
const date = new Date(year, 1 - 1, 1)

date.setDate(days)

return date
}

export default toModernJulianDate
Loading

0 comments on commit 38cbfae

Please sign in to comment.