Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date.toLocaleDateString is not a function

Have simple function which returns an error:

ERROR: date.toLocaleDateString is not a function

TypeError: date.toLocaleDateString is not a function     at FormatTime (../Src/rootdialog.js:87:58) 

Function definition:

function FormatTime(time, prefix = "") {     var date = Date.parse(time);     return ((typeof time != "undefined") ? prefix + date.toLocaleDateString()  : ""); } 

Function receives Date object as input however even explicit conversion to Date with Date.parse() does not help. Using Node.js 8.x. Any solution?

P.S. Issue was caused by BotBuilder architecture.

like image 689
Aleksey Kontsevich Avatar asked Aug 17 '17 00:08

Aleksey Kontsevich


People also ask

What is toLocaleDateString?

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone.

How do I change the date format in toLocaleDateString?

Its pretty straightforward. You can use the date object as follows: var d = new Date(); var mm = d. getMonth() + 1; var dd = d.


Video Answer


1 Answers

Date.parse returns a number. You are looking for new Date. Or, if time already is a Date instance, just use time.toLocaleDateString() (and make sure it really is in every call to the function)!

function formatTime(time, prefix = "") {     return typeof time == "object" ? prefix + time.toLocaleDateString() : ""; } 
like image 88
Bergi Avatar answered Sep 26 '22 13:09

Bergi