使用javascript解析字符串到目前为止 [英] Parsing string to date using javascript

查看:57
本文介绍了使用javascript解析字符串到目前为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,



我有两个字符串值



startdate_string =2 / 21/2017 2017 12:03 PM;

enddate_string =2/21/2017 12:12 PM;



我需要得到毫秒或分钟的差异。



我试图将这两个字符串转换为日期 - 时间值执行以下操作



var startDate = new Date(String(startdate_string));

var endDate = new Date(String( enddate_string));




但是当我在做 endDate-startDate 时,它给了我NaN。我相信我无法将字符串转换为日期时间值。



如何使用JavaScript执行此操作?



谢谢和问候,

Swayam



我尝试过:



startdate_string =2/21/2017 12:03 PM;

enddate_string =2/21/2017 12:12 PM;

var startDate = new Date(String(startdate_string));

var endDate = new Date(String(enddate_string));

console.log(结束日期-的startDate);

解决方案
检查以前的答案:使用javascript将字符串转换为日期 [ ^ ]



RyanDev [ ^ ]是对的 - 删除字符串()围绕字符串日期应该有帮助:

 startdate_string =   2/21/2017 12:03 PM; 
enddate_string = 2/21/2017 12:12 PM;
var startDate = new 日期(startdate_string);
var endDate = new 日期(enddate_string);
console .log(endDate-startDate);


不要让事情变得更复杂。

startdate_string 是一个字符串和字符串(startdate_string)使它成为一个字符串,所以

  var  startDate =  new  日期字符串(startdate_string)); 





  var  startDate =  new  日期(startdate_string); 



是同样的事情。

当代码失败时,确保你可以缩小搜索范围,

一个解决方案就是尝试尽可能多地打印以了解失败的位置。

 startdate_string =   2017/2/21 12:03; 
enddate_string = 2/21/2017 12:12 PM;
var startDate = new 日期(startdate_string);
var endDate = new 日期(enddate_string);
console .log(startDate);
console .log(endDate);
console .log(endDate-startDate);



另一个解决方案是在浏览器中使用调试器或使用FireBug

-----

当你不明白你的代码是什么时做或为什么它做它做的,答案是调试器

使用调试器来查看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


试试这个手册解析



< pre lang =Javascript> function convertToJSDate(datestr)
{

var parts = datestr.split(' ');
var date = parts [ 0 ],time = parts [ 1 ],ampm = parts [ 2 ];
var datepart = date.split(' /');
var timepart = time.split(' );
var hours = timepart [ 0 ];
var minutes = timepart [ 1 ];
if (ampm == PM && hours< 12 )hours = hours + 12 ;
if (ampm == AM && hours == 12 )hours = hours - 12 ;

var fianl = new 日期(datepart [ 2 ],datepart [ 0 ] - 1 ,datepart [ 1 ],小时,分钟, 0 ,< span class =code-digit> 0
);
return fianl;
}
convertToJSDate(' 2/21/2017 12:03 PM' ); // Tue Feb 21 2017 12:03:00 GMT + 0530(India Standard Time)





演示: JSFiddle [ ^ ]


Hello Guys,

I have two string values

startdate_string = "2/21/2017 12:03 PM";
enddate_string = "2/21/2017 12:12 PM";

I need to get the difference in milliseconds or minutes.

I am trying to convert these two strings to date-time value by doing the following

var startDate = new Date(String(startdate_string));
var endDate = new Date(String(enddate_string ));


But when i am doing endDate-startDate, it's giving me NaN. I believe I am not able to convert the strings to Date Time value.

How can i do this using JavaScript?

Thanks and Regards,
Swayam

What I have tried:

startdate_string = "2/21/2017 12:03 PM";
enddate_string = "2/21/2017 12:12 PM";
var startDate = new Date(String(startdate_string));
var endDate = new Date(String(enddate_string ));
console.log(endDate-startDate);

解决方案

Check previous answers: Convert string in to date using javascript[^]

RyanDev[^] is right - removing String() around string-date should help:

startdate_string = "2/21/2017 12:03 PM";
enddate_string = "2/21/2017 12:12 PM";
var startDate = new Date(startdate_string);
var endDate = new Date(enddate_string);
console.log(endDate-startDate); 


Do not make things more complicated than necessary.
startdate_string is a string and String(startdate_string) make it a string, so

var startDate = new Date(String(startdate_string));


and

var startDate = new Date(startdate_string);


are the same thing.
when code fails, make sure of all what you can to narrow the search,
one solution is to try to print as much as possible to know where is the fail.

startdate_string = "2/21/2017 12:03 PM";
enddate_string = "2/21/2017 12:12 PM";
var startDate = new Date(startdate_string);
var endDate = new Date(enddate_string);
console.log(startDate);
console.log(endDate);
console.log(endDate-startDate);


Another solution is to use the debugger in your browser or use FireBug
-----
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


try this manual parsing

function convertToJSDate(datestr)
        {
           
            var parts = datestr.split(' ');
            var date = parts[0], time = parts[1], ampm = parts[2];
            var datepart = date.split('/');
            var timepart = time.split(':');
            var hours = timepart[0];
            var minutes = timepart[1];
            if (ampm == "PM" && hours < 12) hours = hours + 12;
            if (ampm == "AM" && hours == 12) hours = hours - 12; 
            
            var fianl = new Date(datepart[2], datepart[0] - 1, datepart[1], hours, minutes, 0, 0);
            return fianl;
        }
        convertToJSDate('2/21/2017 12:03 PM'); //Tue Feb 21 2017 12:03:00 GMT+0530 (India Standard Time)



demo : JSFiddle [^]


这篇关于使用javascript解析字符串到目前为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆