我有以下代码:
var duration = $('#duration').val();
var now = new Date();
var expire = new Date();
expire.setSeconds(now.getSeconds() + duration);
alert("expire: " + expire + "now: " + now);
var expireString = expire.format("yyyyMMddHHmmssfff");
alert(expireString);
持续时间为60秒。 结果是:
答案 0 :(得分:0)
在javascript中的Date对象上没有格式化方法,这就是为什么你没有得到第二个警报。看看10 ways to format time and date using JavaScript如何实现您想要的目标
答案 1 :(得分:0)
var duration = $('#duration').val();
var now = new Date();
var expire = new Date();
expire.setSeconds(now.getSeconds() + duration);
alert("expire: " + expire + "now: " + now);
var expireString = expire.format("yyyyMMddHHmmssfff");
alert(expireString);
第一个代码块运行正常。
var duration = $('#duration').val();
var now = new Date();
var expire = new Date();
expire.setSeconds(now.getSeconds() + duration);
alert("expire: " + expire + "now: " + now);
另一组块有错误,因为没有构建格式函数,因为第二个警报没有显示,并且给出错误expire.format不是函数。
我想将expire string格式化为某个级别,以支持您提到的时间字符串。你必须这样使用它。
var timestamp = 1391164672;
var pubDate = new Date(timestamp * 1000); //expects milliseconds var pubDate = new Date(); pubDate.setTime(timestamp * 1000);
var weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var formattedDate = weekday[pubDate.getDay()] + ' ' + monthname[pubDate.getMonth()] + ' ' + pubDate.getDate() + ', ' + pubDate.getFullYear() + ' '+ pubDate.toLocaleTimeString();