简体   繁体   English

简单的网络应用程序可在android上运行,但无法在safari上运行

[英]simple web app works on android but doesn't work on safari

I made a simple web app to use at work which takes in a specific date and returns a date X number of days in the future. 我制作了一个简单的Web应用程序以在工作中使用,该应用程序需要一个特定的日期并返回日期X未来的天数。 Works perfect on android. 在android上完美运行。 Safari seems to not be taking in the input values best I can figure. Safari似乎没有吸收我能算出的最佳输入值。 Safari will only return undefined NaN. Safari将仅返回未定义的NaN。 The HTML checks out on the W3C validator. HTML在W3C验证器上签出。 I also tried 'use strict' and let vs const for variables. 我还尝试了“使用严格”,并让vs const作为变量。 I'm now clueless and tired of banging my head. 我现在不知所措,又讨厌撞头。

<head>
    <style>
        body {
            background-color: lightblue;
            text-align: center;
        }

        button {
            margin: 50px 0 25px;
        }
    </style>
</head>
<body>
    <div>
        <h3>Welcome to the</h3>
        <h1>Ko-culator</h1>
    </div>
    <div>
        <p>Enter last fill / pick up date:</p>
        <input type="text" id="lastFillDate" placeholder="M-D-YY" style="text-align: center">
        <p>Enter day supply:</p>
        <input type="text" id="daySupply" placeholder="30, 60, etc" style="text-align: center">
    </div>
    <div>
        <button type="submit" onClick="getNextFill()">Next Fill Date</button>
    </div>
    <div>
        <p class="date-due"></p>
    </div>

    <script>
        function getNextFill() {
            const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            const lastFillDate = new Date(document.querySelector('#lastFillDate').value);
            const daySupply = document.querySelector('#daySupply').value;
            const dayOfMonth = lastFillDate.getDate();
            lastFillDate.setDate(dayOfMonth + Number.parseInt(daySupply));
            const nextFillDate = days[lastFillDate.getDay()] + " " + (lastFillDate.getMonth() + 1) + "-" + lastFillDate.getDate() + "-" + lastFillDate.getFullYear();
            document.querySelector('.date-due').innerHTML = 'Next fill is due on: <br/><br/>' + nextFillDate;
        };
    </script>
</body>

Your code is not working because Safari is more strict regarding date format. 您的代码无法正常工作,因为Safari对日期格式更加严格。 You need to put a valid format. 您需要输入有效的格式。 To test, replace 要测试,更换

const lastFillDate = new Date(document.querySelector('#lastFillDate').value);

with

const lastFillDate = new Date(Date());

and it will work. 它会工作。

Or Enter "2017-09-01" and will work too. 或者输入“ 2017-09-01”,它也将起作用。

It's better to keep your date string in ISO 8601 format (eg "2017-08-16") while creating a Date object with a date string like: 创建带有日期字符串的Date对象时,最好将日期字符串保持为ISO 8601格式(例如“ 2017-08-16”):

new Date("2017-08-16");

Because there are inconsistencies across browsers that you won't expect. 因为您不会期望浏览器之间存在不一致。

In fact, you'll get an Invalid Date while you're trying to create a Date object from a string like "8-16-17" in Safari. 实际上,当您尝试从Safari中的字符串(如“ 8-16-17”)创建Date对象时,您将获得一个无效日期

(You can also find how new Date(dateString) works from here ) (您还可以从此处找到新的Date(dateString)的工作方式)

Meanwhile, DateTime manipulating is a disaster in JavaScript ; 同时,DateTime的操作在JavaScript中一个灾难 I would recommend using library like moment.js ( https://momentjs.com/ ) so that you could write your code in an elegant way, for example: 我建议您使用诸如moment.js( https://momentjs.com/ )之类的库,以便您可以以优雅的方式编写代码,例如:

// Create a date in specific date format
var date = moment("12-25-2017", "MM-DD-YYYY");

// Adding N days
date.add(10, 'days');  // Now it's 2018-01-04

// Output with custom format:
console.log(date.format("MM-DD-YY")); // You'll get: 01-04-18

So that you don't need to handle those existing JavaScript problems on your own. 这样您就无需自己处理那些现有的JavaScript问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM