JavaScript代码,用于显示昨天的日期和今天的日期 [英] Javascript code for showing yesterday's date and todays date

查看:114
本文介绍了JavaScript代码,用于显示昨天的日期和今天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示昨天的日期在我的文本框中的昨天的日期,同时,今天的日期?



我有这个home.php我在哪里显示昨天(用户不能修改此 - 只读)和今天的日期(用户必须输入日期今天)。当明天来,用户访问家庭时,他将会看到昨天输入的日期,并将再次输入日期。



EG:Day1( 2011年3月30日)
昨天的日期:2011年3月29日(不可编辑的文本框)
输入日期今天:(我会输入..)2011年3月30日



第二天(2011年3月31日)
昨天的日期:2011年3月30日(不可编辑的文本框)自动,这将在击中home.php
时显示输入日期今天:(我会输入..)2011年3月31日。



等等..



我需要验证,不接受错误的日期格式,格式必须是:01-Mar-11
如何做? :(

解决方案

昨天的日期只是今天的日期少一个,所以:

  var d = new Date(); 
d.setDate(d.getDate() - 1);
pre>

如果今天是4月1日,那么它将设置为4月0日,转换为3月31日。



因为你也想做一些其他的东西,这里有一些功能要做:

  //检查是否是有效日期
//必须格式年月日名称
//例如2011-MAR-12或2011-March-6
//大写不重要
函数validDate(d){
var bits = d.split(' - ');
var t = stringToDate(d);
返回t.getFullYear()== bits [0] ;&
t.getDate()== bits [2];
}

//将上述格式的字符串转换为日期对象
function stringToDate s){
var bits = s.split(' - ');
var monthNum = monthNameToNumber(bits [1]);
return new Date(bits [0],monthNu m,bits [2]);
}

//将月份名称像mar或march转换为
//数字,大小写不重要
//月号是日历月 - 1
var monthNameToNumber =(function(){
var monthNames =(
'jan feb mar apr may jun jul aug sep oct nov dec'+
'一月二月三月四月四月四月四月六月十一月十一月十月十九日十月十八日(星期六)

返回函数(月){
var i = monthNames.length;
month = month.toLowerCase();

while(i--){
if(monthNames [i] == month){
return i%12;
}
}
}
}());

//给定以上格式的日期,返回
//前一天作为日期对象
函数getYesterday(d){
d = stringToDate(d);
d.setDate(d.getDate() - 1)
return d;
}

//给定一个日期对象,格式为
//每个格式高于
var formatDate =(function(){
var months =' (b)b $ b函数addZ(n){
返回n <10?'0'+ n:''+ n;
}
return function(d){
return d.getFullYear()+' - '+
months [d.getMonth()] +' - '+
addZ(d.getDate());
}
}());

函数doStuff(d){

//格式为year-month-date?
if(!validDate(d)){
alert(d +'not a valid date');
return;
} else {
alert(d +'是有效日期);
}
alert(
'Date in was:'+ d +
'\\\
Day before:'+ formatDate(getYesterday(d))
);
}


doStuff('2011-feb-08');
//显示2011-feb-08是有效的日期
//日期是:2011-feb-08
//前一天:2011-feb-07


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.

E.G: Day1 (march 30, 2011) Yesterday's date: March 29, 2011. (Not editable textbox) Enter date today: (I'll type..) March 30, 2011.

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.

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? :(

解决方案

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.

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

这篇关于JavaScript代码,用于显示昨天的日期和今天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆