0

I have this application where I want to use you date, but the problem is that the date is not working as I expect. I create a date object like this:

// Get today's date
today: function () {

    // Create a new date
    var date = new Date();

    // Set to midnight
    date.setHours(0, 0, 0, 0);

    // Return our date
    return date;
},

and If I output that date in my view I get yesterdays date at 23:00 hours.... Which looks like this:

2015-07-08T23:00:00.000Z

Does anyone know how I can get the date to be formatted properly?

Update

Just to elaborate a bit, I want to use the date to compare against records in the database. These records have the date applied to them, because the JavaScript is showing the local date time, it is not comparing correctly. Also there is a case where I am saving that date and I don't want it to save the local date.

r3plica
  • 13,017
  • 23
  • 128
  • 290

3 Answers3

0

based on your culture setting you can use the

date.toLocaleDateString()

this will give localized string format back

lordkain
  • 3,061
  • 1
  • 13
  • 18
0
date.toUTCString();
date.toLocaleString();
date.toLocaleDateString();
date.toDateString();
date.toISOString();

Find your answer here :) And the best option is to use momentjs http://momentjs.com/

AuthorProxy
  • 7,946
  • 3
  • 27
  • 40
0

So, I ended up creating this function:

// Converts a date to a timeStamp
this.convertToTimeStamp = function (dateTime) {

    // Get just the date
    var date = dateTime.toDateString();

    // Get the timestamp
    var timeStamp = Date.parse(date);

    // Return our timeStamp
    return timeStamp;
};

If my understanding is correct, that should create the same date no matter what timezone / locale you are in.

r3plica
  • 13,017
  • 23
  • 128
  • 290