1

I have seen a few posts about this, but none that I have learnt from.

I need to send a certain date-timestamp to REST API, and it needs to look like this:

2014-01-17 00:00:00.000

So I need two, one for the current months first day, and the current months last day, so something like this:

2014-02-01 00:00:00.000

2014-02-28 00:00:00.000

I have used the following code:

var date = new Date(),
    y = date.getFullYear(),
    m = date.getMonth(),
    firstDay = new Date(y, m, 1),
    lastDay = new Date(y, m + 1, 0),
    querydate = '"_createdAt" : {"$gt" : "' +
        y + '-' + m + '-' + firstDay + ' 00:00:00.000" , "$lt" : "' +
        y + '-' + m + '-' + lastDay + ' 00:00:00.000"}';

But this is the output I get, via dev mode on the browser:

"_createdAt" : {"$gt" : "2014-1-Sat Feb 01 2014 00:00:00 GMT+0000 (GMT) 00:00:00.000" , "$lt" : "2014-1-Fri Feb 28 2014 00:00:00 GMT+0000 (GMT) 00:00:00.000"}}

As you can see, its giving me the correct year, but the month needs to be two digits, and for the last and first day, its giving the full date.

Any ideas how I could manage this ?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Andrew Walker
  • 492
  • 1
  • 5
  • 23
  • 1
    I hate to think how many times this has been covered here on [SO](http://stackoverflow.com/questions/tagged/date-formatting+javascript) :) – Xotic750 Feb 15 '14 at 10:47
  • @Xotic750 The link you posted isn't particularly helpful. – Tomalak Feb 15 '14 at 10:50
  • Your right, I should do the research for him and just feed him an answer. – Xotic750 Feb 15 '14 at 10:52
  • @Xotic750 Looking at other posts, I have not seen once the requirement I am asking. But feel free to post a link that answers it! – Andrew Walker Feb 15 '14 at 10:53
  • One example of formatting a date http://stackoverflow.com/questions/21516995/create-another-formated-date-string-from-an-iso8601-timestamp/21518181#21518181 – Xotic750 Feb 15 '14 at 10:56

4 Answers4

1

Moment.js

For datetime things in javascript I prefer to use moment.js; a small library for nice datetime displaying and calculating.

What you are trying to do would look like this:

moment("2014-05-15 22:15:01").format("YYYY-MM-DD HH:mm:ss") // "2014-05-15, 22:15:01"
Mosselman
  • 1,718
  • 15
  • 27
  • That is how i need it to look. But how do I do that with the last day.? `lastDay = new Date(y, m + 1, 0)`, this gives me the full date rather than just the day digit ? – Andrew Walker Feb 15 '14 at 10:53
1

See the similarity to the other posts now?

Javascript

function padLeft(arg) {
    if (arg < 10) {
        arg = '0' + arg;
    }

    return arg;
}

function myFormat(dateObject) {
    return [
        dateObject.getFullYear(),
        padLeft(dateObject.getMonth() + 1),
        padLeft(dateObject.getDate())
    ].join('-') + ' 00:00:00.000';
}

var date = new Date(),
    y = date.getFullYear(),
    m = date.getMonth(),
    firstDay = new Date(y, m, 1),
    lastDay = new Date(y, m + 1, 0),
    querydate = '"_createdAt" : {"$gt" : "' +
        myFormat(firstDay) + ' , "$lt" : "' +
        myFormat(lastDay) + '"}';

console.log(querydate);

Output

"_createdAt" : {"$gt" : "2014-02-01 00:00:00.000 , "$lt" : "2014-02-28 00:00:00.000"} 

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • I do yes, thank you, but in my defence, its only tagged Javascript, so thats hard to find, and I never saw it in any of my searches. But thank you again :) – Andrew Walker Feb 15 '14 at 11:04
  • I have added those tags to that question now, so it should be easier to find that one along with many others. :) – Xotic750 Feb 15 '14 at 11:06
0
var date = new Date(), 
y = date.getFullYear(),
m = date.getMonth()  
var firstDay = new Date(y, m, 1).getDate() ;
var lastDay = new Date(y, m + 1, 0).getDate()
var querydate = '"_createdAt" : {"$gt" : "'+y+'-'+m+'-'+firstDay+' 00:00:00.000" , "$lt" : "'+y+'-'+m+'-'+lastDay+' 00:00:00.000"}';    
Anup Singh
  • 1,513
  • 3
  • 16
  • 32
  • This is the output I get from the above code `"_createdAt" : {"$gt" : "2014-1-function getDate() { [native code] } 00:00:00.000" , "$lt" : "2014-1-function getDate() { [native code] } 00:00:00.000"}}` ? – Andrew Walker Feb 15 '14 at 10:57
  • I think u are missing someting .. as far I can check I am getting "_createdAt" : {"$gt" : "2014-1-1 00:00:00.000" , "$lt" : "2014-1-28 00:00:00.000"} – Anup Singh Feb 15 '14 at 11:15
0
    var getTimeStamp = function() {
    var tStamp;
    var cd = new Date();
     if ( (cd.getMonth() + 1) < 10 )
        var tStamp = ( cd.getFullYear() + "-0" + (cd.getMonth() + 1) + "-" + cd.getDate() + " 00:00:00.000");
    else
        var tStamp = ( cd.getFullYear() + "-" + (cd.getMonth() + 1) + "-" + cd.getDate() + " 00:00:00.000");
    return tStamp;
    }
Andreas L.
  • 2,805
  • 23
  • 23