用于显示昨天和今天的日期的 Javascript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5495815/
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
Javascript code for showing yesterday's date and todays date
提问by catsgirl008
How to show yesterday's date in my textbox the yesterday's date and at the same time, the today's date in ?
如何在我的文本框中显示昨天的日期和昨天的日期,同时显示今天的日期?
I have this home.php where I show the date yesterday(user cannot modify this-readonly) and the date today(user MUST input the date today). And when tomorrow comes and the user visits the home .php s/he will saw the date inputted yesterday, and will input the date again for romorrow's.
我有这个 home.php,我在其中显示昨天的日期(用户不能修改此只读)和今天的日期(用户必须输入今天的日期)。而当明天到来,用户访问home .php 时,他/他会看到昨天输入的日期,并会再次输入明天的日期。
E.G: Day1 (march 30, 2011) Yesterday's date: March 29, 2011. (Not editable textbox) Enter date today: (I'll type..) March 30, 2011.
EG:第 1 天(2011 年 3 月 30 日)昨天的日期:2011 年 3 月 29 日。(不可编辑文本框)今天输入日期:(我将输入..)2011 年 3 月 30 日。
Day 2 (march 31, 2011) Yesterday's date: March 30, 2011. (Not editable textbox) Automatically, this will appear upon hitting the home.php Enter date today: (I'll type..) March 31, 2011.
第 2 天(2011 年 3 月 31 日)昨天的日期:2011 年 3 月 30 日。(不可编辑的文本框)自动,这将在点击 home.php 时出现今天输入日期:(我将输入..)2011 年 3 月 31 日。
and so on..
等等..
I need a validation that wont accept wrong date format and the format must be: 01-Mar-11 How to do this? :(
我需要一个不会接受错误日期格式的验证,格式必须是:01-Mar-11 如何做到这一点?:(
回答by RobG
Yesterday's date is simply today's date less one, so:
昨天的日期只是今天的日期减去一,所以:
var d = new Date();
d.setDate(d.getDate() - 1);
If today is 1 April, then it is set to 0 April which is converted to 31 March.
如果今天是 4 月 1 日,则将其设置为 4 月 0 日,即转换为 3 月 31 日。
Since you also wanted to do some other stuff, here are some functions to do it:
由于您还想做一些其他的事情,这里有一些功能可以做到:
// Check if d is a valid date
// Must be format year-month name-date
// e.g. 2011-MAR-12 or 2011-March-6
// Capitalisation is not important
function validDate(d) {
var bits = d.split('-');
var t = stringToDate(d);
return t.getFullYear() == bits[0] &&
t.getDate() == bits[2];
}
// Convert string in format above to a date object
function stringToDate(s) {
var bits = s.split('-');
var monthNum = monthNameToNumber(bits[1]);
return new Date(bits[0], monthNum, bits[2]);
}
// Convert month names like mar or march to
// number, capitalisation not important
// Month number is calendar month - 1.
var monthNameToNumber = (function() {
var monthNames = (
'jan feb mar apr may jun jul aug sep oct nov dec ' +
'january february march april may june july august ' +
'september october november december'
).split(' ');
return function(month) {
var i = monthNames.length;
month = month.toLowerCase();
while (i--) {
if (monthNames[i] == month) {
return i % 12;
}
}
}
}());
// Given a date in above format, return
// previous day as a date object
function getYesterday(d) {
d = stringToDate(d);
d.setDate(d.getDate() - 1)
return d;
}
// Given a date object, format
// per format above
var formatDate = (function() {
var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' ');
function addZ(n) {
return n<10? '0'+n : ''+n;
}
return function(d) {
return d.getFullYear() + '-' +
months[d.getMonth()] + '-' +
addZ(d.getDate());
}
}());
function doStuff(d) {
// Is it format year-month-date?
if (!validDate(d)) {
alert(d + ' is not a valid date');
return;
} else {
alert(d + ' is a valid date');
}
alert(
'Date in was: ' + d +
'\nDay before: ' + formatDate(getYesterday(d))
);
}
doStuff('2011-feb-08');
// Shows 2011-feb-08 is a valid date
// Date in was: 2011-feb-08
// Day before: 2011-feb-07
回答by oriadam
One liner:
一个班轮:
var yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000
回答by Codemaker
Yesterday's date can be calculated as,
昨天的日期可以计算为,
var prev_date = new Date();
prev_date.setDate(prev_date.getDate() - 1);
回答by ANKIT-DETROJA
Get yesterday date in javascript
在javascript中获取昨天的日期
You have to run code and check it output
你必须运行代码并检查它的输出
var today = new Date();
var yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
console.log("Original Date : ",yesterday);
const monthNames = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
var month = today.getMonth() + 1
yesterday = yesterday.getDate() + ' ' + monthNames[month] + ' ' + yesterday.getFullYear()
console.log("Modify Date : ",yesterday);
回答by Mustafa Taher Ali
Yesterday Date can be calculated as:-
昨天日期可以计算为:-
let now = new Date();
var defaultDate = now - 1000 * 60 * 60 * 24 * 1;
defaultDate = new Date(defaultDate);