Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript code for showing yesterday's date and todays date

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

like image 954
catsgirl008 Avatar asked Mar 31 '11 05:03

catsgirl008


People also ask

How do you check if a date is after another date in JavaScript?

To check if a date is after another date, compare the Date objects, e.g. date1 > date2 . If the comparison returns true , then the first date is after the second, otherwise the first date is equal to or comes before the second.

How do you code a date in JavaScript?

We can create a date using the Date object by calling the new Date() constructor as shown in the below syntax. Syntax: new Date(); new Date(value); new Date(dateString); new Date(year, month, day, hours, minutes, seconds, milliseconds);

How do you check if the date is today's date in JavaScript?

To check if a date is today's date:Use the Date() constructor to get today's date. Use the toDateString() method to compare the two dates. If the method returns 2 equal strings, the date is today's date.

How to get yesterday’s date in JavaScript?

We will use the toDateString () method to convert the date into a readable string. 2. Get yesterday’s date To calculate yesterday’s date, first we have to get the current date (today) then subtract 1 day from the date. Here, we have used the getDate () function to get the day of the date.

How to get the current date and time in JavaScript?

JavaScript has a built-in Date object that represents the current date and time. First, we will get the current date (Today) and then after we can get the date of yesterday and tomorrow. 1. Get today’s date As we mentioned, we will get today’s date using the date object as shown below in the code.

What is a date object in JavaScript?

Let’s meet a new built-in object: Date. It stores the date, time and provides methods for date/time management. For instance, we can use it to store creation/modification times, to measure time, or just to print out the current date. To create a new Date object call new Date () with one of the following arguments:

What is the Yesterday date of pre-defined date in India?

Here we will get the yesterday date of pre-defined date which is Fri May 10 2019 16:30:00 GMT+0530 (India Standard Time).


2 Answers

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 
like image 124
RobG Avatar answered Oct 16 '22 23:10

RobG


One liner:

var yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000 
like image 45
oriadam Avatar answered Oct 17 '22 00:10

oriadam