Javascript - Date

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

A date object to store date information.

The month is manipulated with January = 0 and not 1

Advertising

3 - Example

  • The now date

// Date.now() returns a numeric value that is used to instantiated today
now = new Date(Date.now());
// Same as:
now = new Date();
console.log("We are the "+now.getDate()+"-"+(now.getMonth()+1)+"-"+now.getFullYear());
console.log("We are the "+now.toString());
console.log("We are the "+now.toDateString());

4 - Management

4.1 - Init

now = new Date(Date.now());
date = new Date(2018,3,30);

4.2 - ToString

There is already a lot of toString function but none of them give the ability to pass a string format.

For example to go to YYYYMMDD

function pad(number) {
  if (number < 10) {
	return '0' + number;
  }
  return number;
}
 
date = new Date(2018,3,30);
 
dateString = date.getFullYear()+''+(pad(date.getMonth()+1))+''+date.getDate();
 
console.log(dateString);

4.3 - Add / Subtstract

var today = new Date();
console.log("Today: "+today);
var tomorrow = new Date(today.getTime()+1000*60*60*24);
console.log("Tomorrow: "+tomorrow);

Advertising

5 - Library

6 - Documentation / Reference

web/javascript/es/date.txt · Last modified: 2019/07/17 17:47 by gerardnico