Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript函数和意外的';楠';_Javascript_Date_Time - Fatal编程技术网

Javascript函数和意外的';楠';

Javascript函数和意外的';楠';,javascript,date,time,Javascript,Date,Time,我尝试编写一个函数,将日期和时间从字符串转换为日期。这是我的密码: var date_one = '2015-08-12 14:15:00'; var date_two = '2015-08-13 15:00:00'; console.log(date_one); //2015-08-12 14:15:00 console.log(date_two); //2015-08-13 15:00:00 var timeStamp_date_one = new Date(date_one).get

我尝试编写一个函数,将日期和时间从字符串转换为日期。这是我的密码:

var date_one = '2015-08-12 14:15:00'; 
var date_two = '2015-08-13 15:00:00';
console.log(date_one); //2015-08-12 14:15:00
console.log(date_two); //2015-08-13 15:00:00


var timeStamp_date_one = new Date(date_one).getTime() ; //NAN?
console.log(typeof timeStamp_date_one);//number 
var timeStamp_date_two = new Date(date_two).getTime() ;//NAN?
console.log(typeof timeStamp_date_two);//number 

//since you are having both datetime in numer time 
//you can compare then to any logical oparation ( >, < ,= ,!= ,== ,!== ,>= AND <=) 


//to be able to work with this date and time agin you need to convert it to an object 
var newTime = new Date(timeStamp_date_one) ;
console.log(typeof newTime) ;//object 

// you can the use this following function to convert your date and time to any format you want 
console.log(DateAndTimeFormat(newTime  , 'time')) ;//  NaN:NaN ???
console.log(DateAndTimeFormat(newTime  , 'date_time')) ;//  NaN/NaN/NaN NaN:NaN ???
function DateAndTimeFormat(dateAndTime, type) {
                switch (type) {
                    case 'time':
                        return dateAndTime.getHours() + ':' + (dateAndTime.getMinutes() < 10 ? '0' : '') + 
                            dateAndTime.getMinutes()
                    case 'date':
                        return dateAndTime.getMonth() + 1 + '/' + dateAndTime.getDate() + '/' +
                            dateAndTime.getFullYear()
                    case 'date_time':
                        return dateAndTime.getMonth() + 1 + '/' + dateAndTime.getDate() + '/' +
                            dateAndTime.getFullYear() + ' ' + dateAndTime.getHours() + ':' +
                            (dateAndTime.getMinutes() < 10 ? '0' : '') + dateAndTime.getMinutes()

                }
            }
var date_one='2015-08-12 14:15:00';
风险值日期=2015-08-13 15:00:00;
控制台日志(日期为1)//2015-08-12 14:15:00
控制台日志(日期为2)//2015-08-13 15:00:00
var timeStamp_date_one=新日期(date_one).getTime()//南?
console.log(时间戳和日期的类型)//数
var timeStamp_date_two=新日期(date_two).getTime()//南?
console.log(时间戳和日期的类型)//数
//因为您在数字时间中有两个日期时间
//然后,您可以将其与指定的任何逻辑运算(>,<,=,!=,=,!=,>=和进行比较,当您像这样处理年份时,格式不是“yyy-MM-DD HH:MM:SS”,而是“YYYY-MM-DDTHH:MM:SS”(注意“T”而不是空格)

有了这些值,代码就可以工作了


这与

有关,我建议先解析表示日期的字符串并将其分为几块,然后使用
新日期(年、月、日、小时、分钟、秒、毫秒);
构造函数100%确保没有任何本地日期格式问题。

可能与
console.log的内容重复(timeStamp_date_one)
输出?您也可以尝试
新日期(“2015/08/12 12:09:36”)
不幸的是,由于我的时区偏移,这在Chrome中为我返回了错误的日期和时间。使用“T”可以很好地工作。输出:14:15 8/12/2015 14:15
var date_one = '2015-08-12T14:15:00'; 
var date_two = '2015-08-13T15:00:00';