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)
3
  • 1
    use ' toLocaleDateString' method of date , example jsfiddle.net/b43k4zdc/2 Commented Mar 25, 2016 at 12:57
  • How did that solve your problem? That solution still will give you the timezone stuff Commented Mar 26, 2016 at 16:57
  • See update in my question Commented Mar 27, 2016 at 10:25

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I need the date variable to be set with 2016/02/28, not to print "2016/02/28"
1

for

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

dt.toDateString()

gives you "Sun Feb 28 2016"

dt.toLocaleDateString()

gives you "2/28/2016"

2 Comments

I need the date variable to be set with 2016/02/28, not to print "2016/02/28"
well all the getters in the api return in string format
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>

4 Comments

I see 2/25/116 - your answer is 1 month behind and the year is not 116
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.
@AaronFranco thats actually the problem of Javascript so i just added 1. As you can see here
It's not a "problem of javascript," it's just the way javascript is. Now your code works properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.