简体   繁体   English

如何将年份转换为日本日历?

[英]How to convert a year to the Japanese calendar?

I have this code that converts the CURRENT Gregorian year to the Japanese era year.我有将当前公历年转换为日本纪元年的代码。

var date = new Date().toLocaleDateString("ja-JP-u-ca-japanese");
var year = date.substring(0, date.indexOf("/"));

However, I want to input the year myself instead of getting the present year.但是,我想自己输入年份而不是获取当前年份。 I tried like this:我试过这样:

var myyear = 2018;
var date = myyear.toLocaleDateString("ja-JP-u-ca-japanese");
var year = date.substring(0, date.indexOf("/"));

It didn't work though.然而它并没有奏效。 How should I do it?我该怎么做?

You can set the year on the Date object:您可以在 Date 对象上设置年份:

 var date = new Date(); date.setFullYear(2018); console.log(date, date.toLocaleDateString("ja-JP-u-ca-japanese"));

I have tested it successfully, you can refer to the below.我已经测试成功了,你可以参考下面的。

在此处输入图片说明

1. Html: Show input text to choose a date in file index.php 1. Html:显示输入文本以在文件 index.php 中选择日期

<input class="form-control date" type="text" value="<?php echo date('Y-m-d'); ?>" autocomplete="off">
<span class="show_date"></span>

2. JS 2. JS

As of now 2021 (reiwa)截至2021年(令和)

$('.show_date').html(toWareki("<?php echo date('Y-m-d');?>"));
    $('.date').datepicker({
            changeYear: true,
            changeMonth: true,
            dateFormat: 'yy-mm-dd',
            locale: 'ja',
            yearRange: "-120:+0",
            onSelect: function(dateText) {
                $('.show_date').text(toWareki(dateText));
            }
        });

function toWareki(dateText) {
        if (!dateText) {
            return '';
        }

        if (dateText.indexOf('/') !== -1) {
            dateText = dateText.replace('/', '-');
        }
        if (dateText.indexOf('/') !== -1) {
            dateText = dateText.replace('/', '-');
        }

        var date_split = formatDate(dateText);
        var dates = date_split.split(',');
        var y = parseInt(dates[0]);
        var m = parseInt(dates[1]);
        var d = parseInt(dates[2]);

        //明治5年以降のみ
        if (y < 1873) {
            return false;
        }

        var date =  formatDate(dateText, 1);
        var label = ''; var localYear = '';
        //日付で分割
        // console.log("date", date);
        if (date >= 20190501) {
            label = '令和';
            localYear = y - 2019 +1; /////
        } else if (date >= 19890108) {
            label = '平成';
            localYear = y - 1988;
        } else if (date >= 19261225) {
            label = '昭和';
            localYear = y - 1925;
        } else if (date >= 19120730){
            label = '大正';
            localYear = y - 1911;
        } else {
            label = '明治';
            localYear = y - 1868;
        }
        //1年は元年
        if (localYear == 1) {
            wareki = label + '元年';
        } else {
            wareki = label + localYear + '年';
        }

        return wareki + m + '月' + d + '日';
    }

    function formatDate(date, option, format) {
        // format is '/' or '-'
        var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;

        var result;
        if (!option) {
            result = [year, month, day].join(format);
        } else {
            result = [year, month, day].join('');
        }
        return result;
    }

Reference links: https://downloadfree.top/23-javascript-code-convert-western-calendar-to-japanese-calendar参考链接: https : //downloadfree.top/23-javascript-code-convert-western-calendar-to-japanese-calendar

You can always do that with the following.您始终可以通过以下方式做到这一点。 DateTime val = new DateTime(your_year_variable, month, year, new GregorianCalendar());

Remember to add System.Globalization namespace at top.请记住在顶部添加 System.Globalization 命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM