简体   繁体   English

Vue2 Date-Picker 日期时间格式为空问题

[英]Vue2 Date-Picker Date Time Format Empty Problem

I'm using vue2-datepicker to represent starting and ending time of meeting in a company.我正在使用 vue2-datepicker 来表示公司会议的开始和结束时间。 In our backend we store the dates in "YYYY-MM-DD HH:mm" format but when we get the data, we convert it on the mounted() hook in "DD-MM-YYYY HH:mm" because in our country this is the correct way to represent the date.在我们的后端,我们以“YYYY-MM-DD HH:mm”格式存储日期,但是当我们获取数据时,我们将其转换为“DD-MM-YYYY HH:mm”中的mounted()钩子,因为在我们的国家这是表示日期的正确方法。

I'm using the same method for all the datepickers but this one bothers me with HH:mm.我对所有的日期选择器都使用了相同的方法,但是这个方法让我很困扰 HH:mm。 When the data comes from API, I use a function called " responseDateTimeFormatter " which slices the YYYY-MM-DD HH:mm formatted date and converts into the one I mentioned earlier.当数据来自 API 时,我使用名为“ responseDateTimeFormatter ”的 function 切片YYYY-MM-DD HH:mm格式的日期并转换为我之前提到的日期。 When I post the data, I also use requestDateTimeFormatter to convert it again as YYYY-MM-DD HH:mm to be stored on database.当我发布数据时,我还使用requestDateTimeFormatter将其再次转换为YYYY-MM-DD HH:mm以存储在数据库中。

Here is the problem, after I convert the data into the format that I want, the <date-picker>s are all empty.这是问题所在,在我将数据转换为我想要的格式后, <date-picker>s Even though their format attribute is set to same one with my date's.即使他们的格式属性设置为与我的日期相同的属性。

Here are my codes:这是我的代码:

DatePicker:日期选择器:

              <date-picker ref="startDatepicker" id="startDate" name="startDate" v-model="meeting.startDate" :first-day-of-week="1" type="datetime" format="DD-MM-YYYY HH:mm" @change="startDateClick" :disabled-date="disableStartDate" :time-picker-options="timePickerOptions"></date-picker>

After I convert the data the v-model looks as follows转换数据后,v-model 如下所示

meetingStartDate = 30-07-2022 09:30

As I explained earlier, it is in YYYY-MM-DD HH:mm format when it came from response.正如我之前解释的,当它来自响应时,它是YYYY-MM-DD HH:mm格式。 I convert it with responseFormatter function below;我用下面的 responseFormatter function 转换它;

  responseTimeFormatter(dateTime) {
    var day = dateTime.slice(8, 10);
    var month = dateTime.slice(5, 7);
    var year = dateTime.slice(0, 4);
    var time = dateTime.slice(11, 16);

    return day + "-" + month + "-" + year + " " + time;
  },

Here is my then() block;这是我的then()块;

  .then((response) => {
    this.meeting = response
    console.log("MEETING", this.meeting)
    this.meeting.startDate = this.responseTimeFormatter(response.startDate)
    console.log("Start Date", this.meeting.startDate)
    
    this.meeting.endDate = this.responseTimeFormatter(response.endDate)
    console.log("End Date", this.meeting.endDate)
  })

So after all these, meeting.startDate is in the right format.因此,在所有这些之后, meeting.startDate 的格式正确。 The v-model of the DatePicker above is in the right format (same with the format attribute of the date picker) but still I am not seeing the date in my date-picker.上面 DatePicker 的 v-model 格式正确(与日期选择器的格式属性相同),但我仍然没有在我的日期选择器中看到日期。 It is empty.它是空的。 在此处输入图像描述

There is no problem when I use it without HH:mm but in datetime format it gives me this problem.当我在没有 HH:mm 的情况下使用它时没有问题,但是在日期时间格式中它给了我这个问题。 Is there anyone have experienced this problem?有没有人遇到过这个问题? What is the solution?解决办法是什么? Thanks in advance.提前致谢。

I checked out the vue2-datepicker package and when I pass string date as input it shows me an empty field so I think it does not accept string type as a date you must pass a date object我检查了vue2-datepicker datepicker package,当我将字符串日期作为输入传递时,它显示了一个空字段,所以我认为它不接受字符串类型作为日期,你必须传递一个日期 object

actually, you don't need that responseTimeFormatter function pass your DateTime string to the Date object, and done:)实际上,您不需要responseTimeFormatter function 将您的 DateTime 字符串传递给 Date object,然后完成:)

modify your then block like this像这样修改你的then

 .then((response) => {
    this.meeting = response
 
    this.meeting.startDate = new Date(response.startDate)
    this.meeting.endDate = new Date(response.endDate)
  })

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

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