This question might sound silly but it really bugs me and I could not find any relevant answers. Is there any way to initialise a javascript variable of date type other than using the new Date()
?. I have the doubt which is similar to this SO question. I need to know if there are any shorter representations.
Asked
Active
Viewed 181 times
-1

Krishna Prashatt
- 631
- 9
- 18
-
why are you looking something for `other than using the new Date()` ? – manikant gautam Apr 03 '19 at 08:09
-
@manikantgautam lets just say that I'm curious. Since we have [] for arrays and {} for objects, I'm curious to kow if there is anything available for date. – Krishna Prashatt Apr 03 '19 at 08:11
-
There is no Date literal. – RobG Apr 03 '19 at 12:04
1 Answers
1
From the MDN Web Docs:
JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.
So no, there is no other way. If you don't like it that way, you can take a look at date libraries like momentjs

Decay42
- 802
- 1
- 9
- 20