0

The following code (see jsfiddle)

var dt = new Date("2016-02-28");
console.log(dt);

logs

Sat Feb 27 2016 19:00:00 GMT-0500 (Eastern Standard Time)

Note 27 vs. 28. I need to create a var that's a date, but how to ignore the timezone?

UPDATE:

This solved my problem: parse manually

var dt = new Date("2016-02-28");
console.log(dt);
var dt2 = new Date(2016,1,28);
console.log(dt2);

logs

Sat Feb 27 2016 19:00:00 GMT-0500 (Eastern Standard Time)
Sun Feb 28 2016 00:00:00 GMT-0500 (Eastern Standard Time)
ps0604
  • 1,227
  • 23
  • 133
  • 330

3 Answers3

1

for

var dt = new Date("2016-02-28");

dt.toDateString()

gives you "Sun Feb 28 2016"

dt.toLocaleDateString()

gives you "2/28/2016"

NullPointerException
  • 3,732
  • 5
  • 28
  • 62
Neo
  • 194
  • 2
  • 8
1

If you are creating the date object in JS, then you can use toLocaleDateString() method, so that the date is formatted as per locale

var dt = new Date("2016-02-28");
console.log(dt.toLocaleDateString());

Output

2/27/2016

reference : Javascript date object

JavaScript toLocaleDateString() Method

NullPointerException
  • 3,732
  • 5
  • 28
  • 62
0

You can just use the getMoneth(), getYear(), and getDate() functions to get your corresponding date. Then i just inserted it into a p tag so you can actually see it!

var d = new Date();
var m = d.getMonth() + 1;
var y = d.getFullYear();
var n = d.getDate();

document.getElementById("result").innerHTML = m + "/" + n + "/" + y
<p id="result">
</p>
amanuel2
  • 4,508
  • 4
  • 36
  • 67
  • I see 2/25/116 - your answer is 1 month behind and the year is not 116 – Aaron Franco Mar 25 '16 at 12:59
  • 1
    It still isn't February. The month in javascript is an Array with starting index a 0. So, if you print the way you are suggesting, you need to add 1 to the month in order to get the appropriate number. – Aaron Franco Mar 25 '16 at 13:02
  • @AaronFranco thats actually the problem of Javascript so i just added 1. As you can see [here](http://www.w3schools.com/jsref/jsref_getmonth.asp) – amanuel2 Mar 25 '16 at 13:03
  • It's not a "problem of javascript," it's just the way javascript is. Now your code works properly. – Aaron Franco Mar 25 '16 at 13:04