如何将一年转换为日历?

时间:2017-11-17 19:41:41

标签: javascript date calendar

我有这个代码将CURRENT Gregorian年转换为日本年。

WITH R
     AS (SELECT referenced_id                                          AS referencing_id,
                referenced_id                                          AS referenced_id,
                CAST(CONCAT('/', referenced_id, '/') AS VARCHAR(8000)) AS referenced_id_path,
                OBJECT_SCHEMA_NAME(referenced_id)                      AS referenced_schema_name,
                OBJECT_NAME(referenced_id)                             AS referenced_entity_name
         FROM   (VALUES (OBJECT_ID('P1'))) V(referenced_id) /*Anchor element*/
         UNION ALL
         SELECT sed.referencing_id,
                CA.referenced_id,
                CAST(CONCAT(R.referenced_id_path, CA.referenced_id, '/')AS VARCHAR(8000)),
                sed.referenced_schema_name,
                sed.referenced_entity_name
         FROM   sys.sql_expression_dependencies AS sed
                INNER JOIN sys.objects AS o
                  ON sed.referencing_id = o.object_id
                INNER JOIN R
                  ON sed.referencing_id = OBJECT_ID(CONCAT(R.referenced_schema_name + '.', R.referenced_entity_name))
                CROSS APPLY (SELECT OBJECT_ID(CONCAT(sed.referenced_schema_name + '.', sed.referenced_entity_name))) CA(referenced_id)
         WHERE  o.type_desc = 'SQL_STORED_PROCEDURE'
                AND R.referenced_id_path NOT LIKE CONCAT('%/', CA.referenced_id, '/%'))
SELECT OBJECT_SCHEMA_NAME(referenced_id),
       OBJECT_NAME(referenced_id),
       referenced_id,
       referenced_id_path
FROM   R 

但是,我想自己输入这一年而不是今年。 我试过这样:

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

虽然没用。我该怎么办?

3 个答案:

答案 0 :(得分:0)

您可以在Date对象上设置年份:

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

答案 1 :(得分:0)

我已经测试成功了,你可以参考下面的内容。

enter image description here

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

截至 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;
    }

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

答案 2 :(得分:-1)

您可以随时使用以下内容执行此操作。 的 DateTime val = new DateTime(your_year_variable, month, year, new GregorianCalendar());

请记住在顶部添加System.Globalization命名空间。