Javascript date.toLocaleDateString 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45724975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
date.toLocaleDateString is not a function
提问by Aleksey Kontsevich
Have simple function which returns an error:
有一个返回错误的简单函数:
ERROR: date.toLocaleDateString is not a function
错误:date.toLocaleDateString 不是函数
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 Dateobject as input however even explicit conversion to Datewith Date.parse()does not help. Using Node.js 8.x. Any solution?
函数接收Date对象作为输入,但是即使显式转换为DatewithDate.parse()也无济于事。使用 Node.js 8.x。有什么解决办法吗?
P.S.Issue was caused by BotBuilder architecture.
PS问题是由BotBuilder 架构引起的。
回答by Bergi
Date.parsereturns a number. You are looking for new Date. Or, if timealready is a Date instance, just use time.toLocaleDateString()(and make sure it really is in every call to the function)!
Date.parse返回一个数字。您正在寻找new Date. 或者,如果time已经是一个 Date 实例,只需使用time.toLocaleDateString()(并确保它确实在每次调用函数中)!
function formatTime(time, prefix = "") {
return typeof time == "object" ? prefix + time.toLocaleDateString() : "";
}
回答by Matt McMahon
You're most likely getting NaNas the result of your Date.parse(time)call.
Check the MDN article on Date.parsefor the types of input strings it accepts if you think your time argument should be valid.
您最有可能获得通话NaN结果Date.parse(time)。如果您认为您的时间参数应该有效,请查看Date.parse上的 MDN 文章,了解它接受的输入字符串的类型。
You may want to modify your return statement so it's checking for failed parses instead of just undefined, e.g.:
您可能想要修改您的 return 语句,以便它检查失败的解析而不是未定义,例如:
function FormatTime(time, prefix = "") {
var date = Date.parse(time); // returns NaN if it can't parse
return Number.isNaN(date) ? "" : prefix + date.toLocaleDateString();
}
回答by RaVi R Pant
function(ng-model_Name,ng-model_Name) {
var fromdate = new Date($scope.ng-model_Name.from.toLocaleDateString());
var todate = new Date($scope.ng-model_Name.to.toLocaleDateString());
return $scope.variable= asign;
}

