JavaScript Time Commands
With JavaScript, the Date function can create, change and partially format dates. JavaScript is based on the time value in milliseconds since January 1, 1970 UTC.
Date Object Spelling Rule:
new Date();
new Date(value);
new Date(datetext);
new Date(year, month[, day[, hour[, minute[, second[, milliseconds]]]]]);
value: It takes the value in milliseconds since January 1, 1970 00:00:00 UTC.
datetext: Accepts date text formatted in Date.parse type.
Year: It takes the year value in 0->1900, 99->1999 or four-digit yyyy format.
month: It takes the value 0 for January and 11 for December.
day: Gets numeric day values of the month.
time: Gets the hours of the day numerically.
minutes: Numerically represents the minutes of an hour.
seconds: seconds of a minute.
milliseconds: gets milliseconds of a second.
Example:
var today= new Date();
var date1 = new Date('December 17, 1995 03:24:00'); //Sun Dec 17 1995 03:24:00 GMT+0200
var date2= new Date('1995-12-17T03:24:00'); //Sun Dec 17 1995 05:24:00 GMT+0200
var date3= new Date(1995, 11, 17); //Sun Dec 17 1995 00:00:00 GMT+0200
var date4= new Date(2005, 11, 17, 3, 24, 0); // Sat Dec 17 2005 03:24:00 GMT+0200
var date5= new Date(2011,0,1); //// Sat Jan 01 2011 00:00:00 GMT+0200
var date6 = new Date('10/25/2014'); //must be entered in month day year format
var date7 =new Date('10 06 2014'); //Mon Oct 06 2014 00:00:00 GMT+0300
When a date is entered in an invalid format, “Invalid Date” or NaN value is returned.
Date Object Methods
new Date('write date 2014').toString(); // returns: "Invalid Date"
Date.parse('date 2014'); // returns: NaN
new Date('write date 2014').toString(); // returns: "Invalid Date"
Date.parse('date 2014'); // returns: NaN
Date.now(): Returns the number of milliseconds since 1970.
Examples: get….
var date = new Date('10/25/2014 14:25:05'); //Sat Oct 25 2014 14:25:05
alert(date.getFullYear()); //2014
alert(date.getDay()); //6
alert(date.getDate()); //25
alert(date.getMinutes()); //25
alert(date.getSeconds()); //05
alert(date.getMilliseconds()); //0
alert(date.getTime()); //1414236305000 (milliseconds from 1970 to the specified date)
Examples: set…
var theBigDay = new Date(1962, 6, 7); // 1962-07-07
theBigDay.setDate(24); // 1962-07-24
theBigDay.setDate(32); // 1962-08-01
theBigDay.setDate(22); // 1962-08-22
var theBigDay = new Date();
theBigDay.setFullYear(1997);
var theBigDay = new Date();
theBigDay.setHours(7);
var theBigDay = new Date('July 1, 1999');
var sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());
var theBigDay = new Date('2015-07-13');
var sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());
var theBigDay = new Date();
theBigDay.setYear(96);
theBigDay.setYear(1996);
theBigDay.setYear(2000);
Printing Days in Turkish with JavaScript
We know that the getDay() command retrieves the days as 0 (Sunday) -6 (Saturday).
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var date=new Date("2012-10-24");
alert( days[ date.getDay() ]); //Wednesday
In order to write much cleaner code and make the code you write more understandable, we can create a method called dayGetir and return the day when you run it. I named the method getDayName (get the day name) so that the code would be suitable for the Date object. I added the method I created to the Date object (Object Oriented JavaScript).
The code I wrote will get the day name when I call getDayName.
I created a method called /*getDayName and connected it to the Date class. A trip is being made.*/
Date.prototype.getDayName=function(){
let dayname=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
return dayname[ this.getDay()];
}
Example
var date=new Date("2012-10-24");
alert( date.getDayName() );
var today =new Date();
alert( today.getDayName());
Writing Months in Turkish with JavaScript
We know that the getMonth() command retrieves the days as 0 (January) – 12 (December).
To print the days on the screen in Turkish, we will create an array named days and print the array element shown by the relevant index (sequence number) on the screen.
var months=["January","February","March","April","May","June","July","August","September","October","November","December "];
var date=new Date("2012-10-24");
alert( months[ date.getMonth() ]);//october
For a much cleaner use, writing a method to get the month name (getMonthName) in the Date class and using it that way would be a very good method for Object-Oriented JavaScript.
Date.prototype.getMonthName=function(){
let monthname=["January","February","March","April","May","June","July","August","September","October","November","December "];
return monthname[ this.getMonth()];
}
var date=new Date("2012-10-24");
alert( date.getMonthName() );
var today=new Date();
alert(today.getMonthName());