使用单独的日期和时间创建新的日期/时间

时间:2017-04-18 13:49:56

标签: javascript date reactjs datetime

我有一个表单设置方式,我从不同的输入框中收集日期,并从dif中收集时间

var appointment_date = new Date();
var appointment_start = new Date("Mon Apr 24 2017 20:00:00 GMT-0400 (EDT)");
var appointment_end = new Date("Mon Apr 24 2017 21:30:00 GMT-0400 (EDT)");

console.log(appointment_date);
console.log(appointment_start);
console.log(appointment_end);

 let selected_day = appointment_date.toString().split(' ').slice(0, 1).join(' '),
        selected_date = appointment_date.toString().split(' ').slice(1, 2).join(' '),
        selected_month = appointment_date.toString().split(' ').slice(2, 3).join(' '),
        selected_year = appointment_date.toString().split(' ').slice(3, 4).join(' ');
    console.log(selected_day);
    console.log(selected_date);
    console.log(selected_month);
    console.log(selected_year);
    
 console.log(appointment_start.toString().split(' ').slice(4, appointment_start.toString().length).join(' '));
 console.log(new Date(selected_year, selected_month, selected_date, appointment_start.toString().split(' ').slice(4, appointment_start.toString().length).join(' ')));
    

不同的输入字段。

我尝试将我的日期和时间转换为字符串;将它们拆分然后再与其他时间相连,但它没有给我适当的时间。有更好的方法吗?

where this.state.appointment_date = new Date();
  this.state.appointment_start = new Date();
  this.state.appointment_end = new Date();

let selected_day = this.state.appointment_date.toString().split(' ').slice(0, 1).join(' '),     // Day
    selected_date = this.state.appointment_date.toString().split(' ').slice(1, 2).join(' '),     // Date
    selected_month = this.state.appointment_date.toString().split(' ').slice(2, 3).join(' '),     // Month
    selected_year = this.state.appointment_date.toString().split(' ').slice(3, 4).join(' ');     // Year
console.log(this.state.appointment_start.setDate(selected_day));  //NaN (output)
console.log(this.state.appointment_start.toString().split(' ').slice(4, this.state.appointment_start.toString().length).join(' ')); // no output
console.log(this.state.appointment_end.toString().split(' ').slice(4, this.state.appointment_end.toString().length).join(' ')); //time

//我尝试使用上述日期创建一个新日期:

console.log(new Date(selected_day, selected_month, selected_date, selected_year, this.state.appointment_start.toString().split(' ').slice(4, this.state.appointment_start.toString().length).join(' ')));

但我得到了invalid date。不知道该怎么做:/

这样我试图将其分解然后再次组合以创建最终日期。但这似乎是一个非常长的方法,应该更简单

预期输入/输出:

  • 输入:日期格式与new Date()

  • 相同
  • 输出:从appointment_date获取日,月和年,从appointment_start获取时间并创建新的日期对象

2 个答案:

答案 0 :(得分:0)

Date的正确语法是 new Date(year, month, day, hours, minutes, seconds, milliseconds); 所以你弄乱了提供的参数。 您案例中的selected_day将生成当天的名称。

答案 1 :(得分:0)

在您的代码中:

var appointment_date = new Date();
var appointment_start = new Date("Mon Apr 24 2017 20:00:00 GMT-0400 (EDT)");
var appointment_end = new Date("Mon Apr 24 2017 21:30:00 GMT-0400 (EDT)");

您不应该使用Date构造函数(或Date.parse)解析字符串,因为它在很大程度上取决于实现。该标准不需要正确解析此格式的字符串。如果要创建一些测试日期,请使用:

var appointment_start = new Date(2017, 3, 24, 20);

假设您的时区是-0400。您的代码的第一部分可以重写为:

var appointment_date  = new Date();
var appointment_start = new Date(2017, 3, 24, 20);
var appointment_end   = new Date(2017, 3, 24, 21, 30);

console.log(appointment_date.toString());
console.log(appointment_start.toString());
console.log(appointment_end.toString());

var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var selected_day   = days[appointment_date.getDay()],
    selected_date  = appointment_date.getDate(),
    selected_month = appointment_date.getMonth() + 1,
    selected_year  = appointment_date.getFullYear();
console.log(selected_day);
console.log(selected_date);
console.log(selected_month);
console.log(selected_year);

let selected_day = appointment_date.toString().split(' ').slice(0, 1).join(' '),

这取决于具有特定格式的 Date.prototype.toString 的输出,但ECMA-262允许格式依赖于实现,因此它可能在不同的主机中具有不同的格式。见Where can I find documentation on formatting a date in JavaScript?

如果您只支持一种格式,请考虑使用库,或编写自己的小格式器。

你在哪里:

console.log(this.state.appointment_start.setDate(selected_day));  //NaN (output)

您传递的值类似于“Tue”(或者可能是完全不同的东西)而不是日期,因此结果为NaN。

在最后一行中你有:

 console.log(new Date(selected_year, selected_month, selected_date,
               appointment_start.toString().split(' ').slice(4,
               appointment_start.toString().length).join(' ')));

尝试通过传递前三个参数的值来构建Date,但随后尝试将所有部分作为单个字符串传递。您需要将所有部件作为单个值传递。此外,月份为零索引,因此您需要减去1。

但是不需要这样的复杂代码,日期的所有部分都可以使用日期方法获得,请参阅上面提到的Where can I find documentation on formatting a date in JavaScript?

相关问题