输入一个生产日期格式"yyyy-MM-dd",再输入一个数字(保质期的天数)。然后经过计算输出促销日期。促销日期为:该商品过期日前2周的周三

<!--
 * @FilePath: 促销日期
 * @Author: 马小屁
 * @Date: 2022-08-19 20:05:49
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-08-20 16:41:52
 * Copyright: 2022 xxxTech CO.,LTD. All Rights Reserved.
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //生产日期
        var scDate = new Date(prompt('请输入生产日期,输入格式为yyyy-MM-DD'));
        // 保质期天数的毫秒数
        var bzSeconds = prompt('请输入保质期天数') * 1000 * 24 * 60 * 60;
        //过期日日期的毫秒数
        var overSeconds = scDate.getTime() + bzSeconds;
        //过期日前两周日期的毫秒数
        var beforeWeekSeconds = overSeconds - 14 * 1000 * 60 * 60 * 24;
        // 过期日前两周日期
        var time = new Date(beforeWeekSeconds);
        //过期日前两日期当天星期几
        var week = time.getDay();
        //促销日
        var promotionDay;
        switch (week) {
            case 0:
                promotionDay = new Date(beforeWeekSeconds + 3 * 1000 * 60 * 60 * 24)

                break;
            case 1:
                promotionDay = new Date(beforeWeekSeconds + 2 * 1000 * 60 * 60 * 24)
                break;
            case 2:
                promotionDay = new Date(beforeWeekSeconds + 1 * 1000 * 60 * 60 * 24)
                break;
            case 3:
                promotionDay = time
                break;
            case 4:
                promotionDay = new Date(beforeWeekSeconds + 6 * 1000 * 60 * 60 * 24)
                break;
            case 5:
                promotionDay = new Date(beforeWeekSeconds + 5 * 1000 * 60 * 60 * 24)
                break;
            case 6:
                promotionDay = new Date(beforeWeekSeconds + 4 * 1000 * 60 * 60 * 24)
                break;
        }
        alert(`促销日为${promotionDay.toLocaleDateString()}`);






    </script>
</body>

</html>

 

posted @ 2022-08-20 20:03  丹江路39号  阅读(241)  评论(0)    收藏  举报