简体   繁体   English

Moment JS(“必须显示下周的几天”)

[英]Moment JS (“Must to show days in next week”)

I'm working on a weather service and I need to write a code for showing the weather forecast for next week.我正在研究气象服务,我需要编写一个代码来显示下周的天气预报。 From the server, I only have a "time" entity with "value" of next Monday like "2020-04-06T00:00:00.000+02:00".在服务器上,我只有一个“时间”实体,其“价值”为下周一,如“2020-04-06T00:00:00.000+02:00”。

{
    "message": "Show me the weather for next week",
    "entities": [
        {
            "value": "weather",
            "text": "weather",
            "entity": "radio_tag"
        },
        {
            "value": "play_next",
            "text": "next",
            "entity": "media_control"
        },
        {
            "value": "next",
            "text": "next",
            "entity": "next"
        },
        {
            "value": {
                "values": [
                    {
                        "value": "2020-04-06T00:00:00.000+02:00",
                        "grain": "week",
                        "type": "value"
                    }
                ],
                "value": "2020-04-06T00:00:00.000+02:00",
                "grain": "week",
                "type": "value"
            },
            "entity": "time"
        },
        {
            "value": "weather_condition",
            "entity": "sub_intent"
        }
    ],
    "intent": "weather_condition",
    "user_id": "5258",
    "language_id": "56",
    "driver": "gigaaa",
    "abort": "0",
    "extras": {
        "coordinates": {
            "lat": "44.4150804",
            "lng": "19.1290729"
        }
    },
    "conversation_messages_count": "1"
}

For example, if today is Tuesday, I must show Monday and Tuesday for next week, if today is Saturday (04.04), I must show every day from Monday (06.04) to Saturday(11.04) for next week.例如,如果今天是星期二,我必须显示下周的星期一和星期二,如果今天是星期六(04.04),我必须显示下周星期一(06.04)到星期六(11.04)的每一天。 I am a beginner and I must use Moment JS for this.我是初学者,为此我必须使用 Moment JS。

I tried this condition if (this.conversation.payload.grain === 'week' && moment().startOf('isoWeek').isSame(this.conversation.payload.forecastTime)) but the second part of condition is not correct.我试过这个条件if (this.conversation.payload.grain === 'week' && moment().startOf('isoWeek').isSame(this.conversation.payload.forecastTime))但条件的第二部分不是正确的。

We take the current date and check if we aren't in the same isoweek as the data in order to filter out data rows from the current week.我们获取当前日期并检查我们是否与数据不在同一个等周,以便从当前周中过滤掉数据行。 We then take the current date and add a week so we can check that <1 week has passed.然后,我们获取当前日期并添加一周,以便我们可以检查是否已过去 <1 周。 Theoretically, as long as the timezone of the user and the returned data are the same, it should be what you need.从理论上讲,只要用户的时区和返回的数据相同,就应该是你需要的。

var valueFromData = this.conversation.payload.value;
var dateFromData = moment(valueFromData).startOf('day');
var oneWeek = moment().add(7,'d').startOf('day');
if(moment().startOf('isoweek').isBefore(dateFromData)
   && dateFromData.isBefore(oneWeek))

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

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