在JavaScript中,利用三元运算符生成当前日期yyyy-MM-dd

<script type="text/javascript">
    //得到当前时间yyyy-MM-dd
    var myDate = new Date();
    var nowDateTime = myDate.getFullYear();
    nowDateTime = nowDateTime + ((myDate.getMonth() + 1) < 10 ? "-0" + (myDate.getMonth() + 1) : "-" + (myDate.getMonth() + 1));
    nowDateTime = nowDateTime + (myDate.getDate() < 10 ? "-0" + myDate.getDate() : "-" + myDate.getDate());
    
    alert(nowDateTime);
</script>

  

你可能感兴趣的:(在JavaScript中,利用三元运算符生成当前日期yyyy-MM-dd)