Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Comparing dateTime value and string dateTime
Message
General information
Forum:
Javascript
Category:
Other
Miscellaneous
Thread ID:
01620449
Message ID:
01620521
Views:
40
You have to normalize those dates either both as strings or both as dates. Most date picker controls should have a way to convert an input date into an actual date.

You can easily convert ISO dates (the string with the T in it) into dates by using:
var stringDate = "2015-05-28T12:31:06.217";
var date = new Date(stringDate);
I believe JavaScript is also smart enough to parse the date without the T and a space instead. I just tried this in the Node CLI and it new Date() converts either of your dates to real date values.

However - I wonder why you're doing your date parsing and validation on the client. I think it's a better idea to leave that for server validation. Personally I try to deal with all date processing on the server and present dates in the UI only as strings.

Otherwise if you do need to do lots of date manipulation and presentation via client code you might want to look at moment.js which provides solid date display and parsing functionality.

+++ Rick ---


>Hi everybody,
>
>In the interface I have a dateTime picker object with the date set to be "2011-04-07 18:37:30.000" (apparently, it's a string date).
>
>And I also have an object which has dateTime property which is set to be in this format
>
>
>
(this is I see when I output it to the console).
>
>I have this code
>
>
>if ($scope.startDate!=null && $scope.filteredTransactions[i].dateTime < $scope.startDate)
>                            {
>                                $scope.filteredTransactions.splice(i,1); // remove that element
>                                continue;
>                            }
>
>                            //window.console && console.log('i is ' + i + ' Transaction Date is ' + $scope.filteredTransactions[i].dateTime + ' endDate is ' + $scope.endDate);
>                            if ($scope.endDate!=null && $scope.filteredTransactions[i].dateTime > $scope.endDate)
>                            {
>                                $scope.filteredTransactions.splice(i,1); // remove that element                                
>                            }
>
>and it doesn't seem to work correctly for the time portion of the date.
>
>Do you know what should I do to be able to properly compare these two values?
>
>
>Also, for both values angular.isDate function returns false but angular.isString returns true, so both are treated as strings, I think.
>Thanks in advance.
>
>I am thinking that I need to create date object of both of them (e.g. dateTime property and my range dates). new Date() of both seem to work, so that's what I'm going to try now.
>
>UPDATE. It seems to work but I had to add replace('T',' ') to the dateTime object, otherwise it was automatically converted to UTC.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform