douxie7339 2018-06-06 15:09
浏览 51
已采纳

Javascript中的月/日比较

I'm not an expert in JavaScript, but I've been tasked with migrating a very large website that was coded into PHP into a new CMS that does not allow server-side languages. I am therefore spending my days converting many PHP date-calcs to JavaScript.

Something very simple in PHP:

<?php 
if (date('md') < 816) {$award_year = date('Y');}
if (date('md') > 815) {$award_year = date('Y') + 1;}
/*echo "year ".$award_year;*/
?>

This allows the year an application is due to automatically change to next year after August 15.

I've been trying to recreate this effect with Javascript and here is what I have come up with:

var today = new Date();
var mm = today.getMonth()+1; //January is 0! 
var dd = today.getday();
if (mm < 10) {
    mm = '0'+mm
} 

currDate = mmdd;
var currDate = new Date(); 
var appDate = new Date("0816");

if (currDate < appDate){
var printDate = theDate.getFullYear();  
}

else if (currDate >= appDate){
var printDate = theDate.getFullYear()+1;      
}

I know that I am missing something, because the var currDate cannot just = mmdd and then be compared to another date. Can someone help me with the next step here ? I'm trying to actually learn JavaScript as I go rather than just blindly fix issues.

  • 写回答

3条回答 默认 最新

  • doushijia5684 2018-06-06 15:44
    关注

    This will look similar to your PHP code:

    const date = new Date();
    const day = date.getUTCDate();
    const month = date.getMonth();
    const md = month + "" + day;
    
    if (md < 816) {
        var award_year = date.getFullYear();
    }
    else if (md > 815) {
        var award_year = date.getFullYear() + 1;
    }
    
    console.log(award_year);

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘