问题描述
我希望仅在昨天的日期返回日期。没有秒,
毫秒。格式化为yyyy / mm / dd或mm / dd / yyyy。 VB这样做很容易
日期() - 1如果今天是03/28/2007将返回03/27/2007。为什么如此
javascript的许多箍?有什么想法吗?
I''m looking to return DATE ONLY for yesterday''s date. No seconds,
milliseconds. Formatted either yyyy/mm/dd or mm/dd/yyyy. VB does it so
easily Date()-1 will return 03/27/2007 if today is 03/28/2007. Why so
many hoops for javascript? Any ideas?
推荐答案
切勿使用后一种格式。这对全世界的观众来说非常困惑。
var x = new Date();
var s = x.getFullYear()+'' /''+ x.getMonth()+''/''+ x.getDate();
alert(s);
Never use the latter format. It ''s highly confusing to a worldwide audience.
var x = new Date();
var s = x.getFullYear() + ''/'' + x.getMonth() + ''/'' + x.getDate();
alert( s );
这是一个非常好的问题。我想答案包括Javascript通过将所有格式保留给我们来提供的灵活性。
hth
ivo
我忘了减去一天,还有一行代码:
var x = new Date ();
x.setDate(x.getDate() - 1);
var s = x.getFullYear()+''/''+ x.getMonth ()+''/''+ x.getDate();
alert(s);
hth
ivo
或
var x = new Date();
var s = x.getFullYear()+''/''+ x.getMonth()+''/ ''+(x.getDate() - 1);
alert(s);
or
var x = new Date();
var s = x.getFullYear() + ''/'' + x.getMonth() + ''/'' + (x.getDate()-1);
alert( s );
这篇关于javascript相当于vbscript Date() - 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!