jQuery get无法为我带来非字符串变量
这是我的密码jQuery get无法为我带来非字符串变量,jquery,Jquery,这是我的密码 jQuery.get('http://example.com/text.txt', function(data) { var newDate = new Date(data); /* .... */ }); My text.txt包含以下格式的日期:2015,5,28,20,10(年、月、日、小时、分钟)。问题是我想要这个: var newDate = new Date(2015,5,28,20,10); 但数据实际上是一个字符串。所以,我想我得
jQuery.get('http://example.com/text.txt', function(data) {
var newDate = new Date(data);
/*
....
*/
});
My text.txt包含以下格式的日期:2015,5,28,20,10(年、月、日、小时、分钟)。问题是我想要这个:
var newDate = new Date(2015,5,28,20,10);
但数据实际上是一个字符串。所以,我想我得到了这个:
var newDate = new Date("2015,5,28,20,10");
谢谢你的帮助 **更新-这不起作用**接受来宾271314的回答 这并不理想,因为您的数据文本必须是正确的格式(尽管您可以编写一些错误处理)。必须将该日期字符串设置为数字才能使date()正常工作。因此,从字符串中删除逗号,然后将字符串转换为数字。像这样:
//here is your string from the txt file
var string = "2015,5,28,20,10"
//remove the commas
var nums = string.replace(/,/g , "");
//convert the string to a number
nums = Number(nums)
//get the date
var newDate = new Date(nums);
这里有一把小提琴试着利用.split()
,.map()
,.apply()
var data=“2015,5,28,20,10”。拆分(/,/).map(函数(n){
返回编号(n)
});
var newDate=新日期(数据[0],数据[1],数据[2],数据[3],数据[4]);
文件编写(newDate)代码>控制台说什么<代码>控制台日志(数据)代码>?有任何错误吗?控制台显示:2015,5,28,20,10谢谢您的帮助!:)等等,这是错的,对不起。请接受271314的回答谢谢。在Cory的回答之后,我使用了:var array=data.split(“,”);对于(数组中的num){num=Number(num);}var newDate=newDate(数组[0],数组[1],数组[2],数组[3],数组[4])代码>