Help us understand the problem. What is going on with this article?

誕生日から年齢、曜日、干支、誕生石、星座を求める JXA(JavaScript for Automation)

概要

 AppleScript のスクリプトエディタで動作する、JavaScript です。(OS X で動作します。)
誕生日を入力し、基準日(省略すると現在日付)現在の年齢を計算します。
誕生日の曜日、干支、誕生石、星座も表示します。
誕生日の入力は、和暦:"元号yy年mm月dd日"、西暦:"yyyy/mm/dd" どちらの形式でもOKです。

 Scriptフォルダに入れとくと、メニューバーから起動できて便利に使えると思います。

ソース

 スクリプトエディタの画面で、JavaScript に設定してソースをコピし、保存します。

年齢計算Javascript.scpt
// 誕生日の曜日と年齢、干支、誕生石、星座を求める。
var app = Application.currentApplication()
app.includeStandardAdditions = true

var ans = app.displayDialog("誕生日を入力 (yyyy/mm/dd か 元号nn年mm月dd日)", {defaultAnswer: ""})
var iDate = ans.textReturned
var sDate = new Date(gengoChack(iDate))
var ans = app.displayDialog("計算の基準日を入力 (yyyy/mm/dd)入力を省略すると今日を基準に計算します。", {
    defaultAnswer: ""})
if (ans.textReturned == "") var tDate = new Date()
    else var tDate = new Date(ans.textReturned)

var youbi = '日月火水木金土'[sDate.getDay()]
var age = tDate.getFullYear() - sDate.getFullYear()
var mDate = new Date(tDate.getFullYear() + "/" + (sDate.getMonth() + 1) + "/" + sDate.getDate())
if (tDate < mDate) age = age - 1
ans = tDate.getFullYear() + "/" + (tDate.getMonth() + 1) +"/" + tDate.getDate()
var eto = ('申酉戌亥子牛寅卯辰巳午未')[sDate.getFullYear()%12]

app.displayDialog(iDate + " " + youbi + "曜日、" + eto + "年生まれのあなたは\n" +
    yymmdd(tDate) + "現在の年齢は " + age + "才で、\n" +
    "誕生石は" + stone(sDate.getMonth()) +
    "、星座は" + seiza((sDate.getMonth() + 1), sDate.getDate()) + "です。")

function yymmdd(wDate) {
    return wDate.getFullYear() + "/" + (wDate.getMonth() + 1) + "/" + wDate.getDate()
}

function stone(MM) {
    var ishi = ["ガーネット","アメジスト","アクアマリン","ダイヤモンド","エメラルド","真珠","ルビー",
            "ペリドット","サファイア","オパール","トパーズ","タンザナイト"]
    return ishi[MM]
}

function seiza(MM, DD) {
    var seiza = ""
    if (MM == 3 && DD >= 21) ans  = "牡羊"
    if (MM == 4 && DD <=19) ans  = "牡羊"
    if (MM == 4 && DD >= 20) ans  = "牡牛"
    if (MM == 5 && DD <=20) ans  = "牡牛"
    if (MM == 5 && DD >= 21) ans  = "双子"
    if (MM == 6 && DD <=21) ans  = "双子"
    if (MM == 6 && DD >= 22) ans  = ""
    if (MM == 7 && DD <=22) ans  = ""
    if (MM == 7 && DD >= 23) ans  = "獅子"
    if (MM == 8 && DD <=22) ans  = "獅子"
    if (MM == 8 && DD >= 23) ans  = "乙女"
    if (MM == 9 && DD <=22) ans  = "乙女"
    if (MM == 9 && DD >= 23) ans  = "天秤"
    if (MM == 10 && DD <=23) ans  = "天秤"
    if (MM == 10 && DD >= 24) ans  = ""
    if (MM == 11 && DD <=21) ans  = ""
    if (MM == 11 && DD >= 22) ans  = "射手"
    if (MM == 12 && DD <=21) ans  = "射手"
    if (MM == 12 && DD >= 22) ans  = "山羊"
    if (MM == 1 && DD <=19) ans  = "山羊"
    if (MM == 1 && DD >= 20) ans  = "水瓶"
    if (MM == 2 && DD <=18) ans  = "水瓶"
    if (MM == 2 && DD >= 19) ans  = ""
    if (MM == 3 && DD <=20) ans  = ""
    return ans + ""
}

function gengoChack(ymd) {
    var gengo = ymd.substr(0,2)
    var dif
    switch(gengo) {
        case '明治': dif = 1867; break; // 元号の開始年 -1 を設定
        case '大正': dif = 1911; break;
        case '昭和': dif = 1925; break;
        case '平成': dif = 1988; break;
        case '令和': dif = 2018; break;
        default: return (ymd);  // 西暦をそのまま返す
    }
    var nen = ymd.split(gengo)[1].split('')[0]
    var tuki = ymd.split('')[1].split('')[0]
    var hi = ymd.split('')[1].split('')[0]
    nen = Number(nen) + Number(dif)
    return nen + "/" + tuki + "/" + hi  // 和暦を西暦に変換した値を返す
}

コメント歓迎

 もっとエレガントな方法がありましたら、是非コメントをお願いします。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした