Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted
Updated 21-Feb-17 20:10pm
Comments
[no name] 21-Feb-17 14:51pm    
Try without the "PM"
ZurdoDev 21-Feb-17 14:58pm    
It works fine for me. Also remove String() since that is redundant. I get 540000.

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

RyanDev[^] is right - removing String() around string-date should help:
JavaScript
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); 
 
Share this answer
 
Do not make things more complicated than necessary.
startdate_string is a string and String(startdate_string) make it a string, so
JavaScript
var startDate = new Date(String(startdate_string));

and
JavaScript
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.
JavaScript
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.
 
Share this answer
 
try this manual parsing

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 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 [^]
 
Share this answer
 
v2
Never do that. You cannot reliably work on Date as string. Instead, use JavaScript Dates[^]. Refer to my previous answer[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900