Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Object Behaviour [duplicate]

In JavaScript, Result of the Date Object is different when it comes to Different Time Zones and Different Notation.

eg.

var res= "2018-08"; 
var dat=new Date(res);  

Result are :

Tue Jul 31 2018 22:00:00 GMT-0200 (West Greenland Summer Time)  for GMT-0200  Time Zone 

and

 Wed Aug 01  2018 05:30:00 GMT+0530 (India Standard Time)

But When

var res= "2018/08"; 

We are getting same date

Wed Aug 01 2018 00:00:00 GMT+0530 (India Standard Time)

Wed Aug 01 2018 00:00:00 GMT-0300 (Atlantic Daylight Time)

Can Anyone Explain me Why its not Constant when we gave "-" as delimiter

like image 285
murari setty gnana vinay Avatar asked Nov 08 '22 03:11

murari setty gnana vinay


1 Answers

In order for the object to understand the format you're feeding it, is by actually telling it the format you're feeding it.

The below might help you achieve what you're after:

Option 1:

Using date set methods or using the Date.parse() functionality (if that satisfies your needs).

Option 2:

Using something ott similar to momentjs that will do the logic for you.

like image 108
Julian Camilleri Avatar answered Nov 12 '22 12:11

Julian Camilleri