0

I want to develop a JavaScript function to calculate the activity of users based on the date in the server where the data is stored. The problem is that the date is a string like this:

2013-08-11T20:17:08.468Z

How can I compare two string like this to calculate minor and major time as in the example?

Joshua Dwire
  • 5,415
  • 5
  • 29
  • 50
Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96
  • 2
    What do you mean by minor / major time ad ? If you want to compare times, you should convert your date to UNIX epoch like this : `new Date(myDateString).getTime()`. – m_vdbeek Aug 12 '13 at 13:44
  • 2
    Have you tried anything yet? You should be able to write just a few lines to compare times, using the [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object and its [`.getTime()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2FgetTime) method. – ajp15243 Aug 12 '13 at 13:45
  • Your database is probably capable of comparing the dates for you, if you need to do it client side you can probably get the db to return the output in something easier to compare – NickSlash Aug 12 '13 at 13:45
  • Use valueOf method that gives you milliseconds. so you can easily compare. – Jay Shukla Aug 12 '13 at 13:48

4 Answers4

1

If you want to compare two dates just use this :

var dateA = '2013-08-11T20:17:08.468Z';
var parsedDateA = new Date(dateA).getTime();

var dateB = '2013-06-06T17:33:08.468Z';
var parsedDateB = new Date(dateB).getTime();

if(parsedDateA > parsedDateB) {
    // do something
}
m_vdbeek
  • 3,704
  • 7
  • 46
  • 77
0

Assuming you need to do the comparisons client-side, the best way is to load the dates into Date objects using Date.parse. Then compare them using the functions provided for Date, such as getTime.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0

Try parse method:

var s = "2013-08-11T20:17:08.468Z";
var d = Date.parse(s);
rook
  • 5,880
  • 4
  • 39
  • 51
  • I did link on `w3schools`, but just realized that here should be [`disclaimer`](http://www.w3fools.com/) – rook Aug 15 '13 at 19:49
0

As I have understood you in the right way, there is a good answer to your question here.

You can also look at this very good Library (DateJS).

If your problem was converting from the Date-String to js-Date look at this Page.

Community
  • 1
  • 1
Martin
  • 3,096
  • 1
  • 26
  • 46
  • [Link-only answers are highly discouraged](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) – ajp15243 Aug 12 '13 at 14:48
  • @ajp15243 I know this. But should I type the whole answer again, copy the DateJS code or copy from the msdn help sites? I think copies of other members to nearly same questions are also not couraged. – Martin Aug 13 '13 at 07:56
  • You don't have to do a direct copy/paste, but you can use the information in those links to type your own answer. There's nothing wrong with what is essentially a duplicate answer if you're using someone else's answer to inform your own (assuming their answer is correct). Also, if your answer will end up being a duplicate, then it's likely that the question is a duplicate (which is a problem with the question, not your answer). There's also nothing wrong with long (informative) answers, and nothing wrong with "further reading" links (like the lib and MSDN sites) alongside an answer. – ajp15243 Aug 13 '13 at 18:05
  • Ok I understand your argumentation. And yes I dislike only-link-answers too. But my problem was, that I do not want to get reputation which should be given to the person who did the whole research and posts the long answer. – Martin Aug 14 '13 at 06:15