Skip to content

Instantly share code, notes, and snippets.

@Restoration
Created October 2, 2023 06:12
Show Gist options
  • Save Restoration/0269b1b30d04971b85195433f83f3764 to your computer and use it in GitHub Desktop.
Save Restoration/0269b1b30d04971b85195433f83f3764 to your computer and use it in GitHub Desktop.
function parseJSTDate(dateString: string): Date | null {
// 正規表現を使用して日付と時刻の情報を取得
const match = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.exec(dateString);
if (!match) {
return null;
}
// 取得した情報を使用して新しいDateオブジェクトを作成
const year = parseInt(match[1], 10);
const month = parseInt(match[2], 10) - 1; // JSの月は0から始まる
const day = parseInt(match[3], 10);
const hours = parseInt(match[4], 10);
const minutes = parseInt(match[5], 10);
const seconds = parseInt(match[6], 10);
return new Date(year, month, day, hours, minutes, seconds);
}
const input = "2023-10-02 15:09:48.000000000 +0900 JST";
const dateObj = parseJSTDate(input);
if (dateObj) {
console.log(dateObj);
} else {
console.error("Invalid date format");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment