0

yyyy-mm-dd以前にPHPで行ったJSを介した形式で日付を渡す方法を理解しようとしていますが、PHPとJSはこの意味で異なります。私は少し途方に暮れています。

私がPHPで行った方法は次のとおりです。

var default_dob = strtotime(date('m/d/Y', time()) .' -18 year');
var dob = date('m/d/Y', default_dob);

基本的に、今日の日付を取得し、18 年を減算しmm/dd/yyyyて、日付ピッカー用に再フォーマットします。理想的には、既に大量の JS スタックに別のプラグインを追加することは避けたいと考えています。だから私が余分な重量なしでこれを行うことができれば(それをおそらく既製の機能に差し込むことができることを除いて)私は幸せになるだろう.

4

3 に答える 3

1

これにより、正確に 18 年前の日付が必要な形式で通知されます。

var date = new Date(); 
date.setFullYear(date.getFullYear() - 18); 
alert(date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate());
于 2013-09-02T09:49:12.090 に答える
0
// Left pad a string to the specified length using the specified character
function padLeft(str, length, char)
{
    // Make sure args really are strings and that the length is a positive
    // number. If you don't do this concatenation may do numeric addition!
    str = String(str);
    char = String(char) || ' '; // default to space for pad string
    length = Math.abs(Number(length));
    if (isNaN(length)) {
        throw new Error("Pad length must be a number");
    }

    if (str.length < length) {
        // Prepend char until the string is long enough
        while (str.length < length) {
            str = char + str;
        }

        // Make sure the string is the requested length
        return str.slice(length * -1);
    } else {
        // The string is already long enough, return it
        return str;
    }
}

// Get the current date/time
// This is local to the browser, so it depends on the user's system time
var default_dob = new Date();

// Subtract 18 years
default_dob.setFullYear(default_dob.getFullYear() - 18);

// Format the string as you want it. PHP's d and m formats add leading zeros
// if necessary, in JS you have to do it manually.
var dob = padLeft(default_dob.getMonth(), 2, '0') + '/'
        + padLeft(default_dob.getDate(), 2, '0') + '/'
        + default_dob.getFullYear()

参照:オブジェクトの MDN エントリDate()

于 2013-09-02T09:53:05.123 に答える
0

これを試して

<script type="text/javascript">
$ss=  date('m/d/Y', strtotime('+18 year'));
?>
var default_dob = '<?php echo $ss;?>';
alert(default_dob);
</script>
于 2013-09-02T09:53:10.907 に答える