Object Date в JavaScript. Its properties and methods. Code ... open

Object Date в JavaScript. Its properties and methods. Code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

The Date object allows you to work with dates and times in JavaScript.

Let’s create a Date object:

    var currentDate = new Date()

In the constructor, we can pass the number of milliseconds that have passed since the beginning of the Unix era ( January 1, 1970 00:00:00
GMT
):

    var newDate = new Date(1359270000000); // Sun Jan 27 2013 10:00:00 GMT+0300 (RTZ 2 )

Let’s transfer the day, month and year to the contractor:

    var myDate = new Date("27 March 2008");//Thu Mar 27 2008 00:00:00 GMT+0300 (RTZ 2 (winter))
    var myDate = new Date("3/27/2008");//Thu Mar 27 2008 00:00:00 GMT+0300 (RTZ 2 (winter))

Let’s pass all the parameters:

    var myDate = new Date(2012, 11, 25, 18, 30, 20, 10); // Tue Dec 25 2012 18:30:20 GMT+0300 (RTZ 2 (winter))

In this case, the following parameters are used in order: new Date(year, month, day, hour, minutes, seconds, milliseconds).
In this case, it should be borne in mind that the countdown of months starts from zero, that is, January is 0, and December is 11.

Getting date and time

A number of methods are used to obtain the various components of a date:

  • getDate – returns the day of the month
  • getDay – returns the day of the week (counting starts from 0 – Sunday, and the last day – 6 – Saturday)
  • getMonth – returns the number of the month(the countdown starts from zero, that is, the month from number 0 is January)
  • getFullYear – returns the year
  • toDateString – returns the full date as a string
  • getHours – returns the hour (from 0 to 23)
  • getMinutes – returns minutes (from 0 to 59)
  • getSeconds – returns seconds (from 0 to 59)
  • getMilliseconds – returns milliseconds (from 0 to 999)
  • toTimeString – returns the total time as a string

Get the current date:

    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

    var myDate = new Date();
    var fullDate = "Today: " + myDate.getDate() + " " + months[myDate.getMonth()] +
        " " + myDate.getFullYear() + ", " + days[myDate.getDay()];
    document.write(fullDate);//Today: 18 August 2015, Tuesday

Setting the date and time

In addition to setting the date parameters in the constructor for setting, we can also use additional methods of the object date:

  • setDate – setting day to date
  • setMonth– setting the month (the countdown starts from zero, that is, the month from number 0 is January)
  • setFullYear – sets the year
  • setHours – hour setting
  • setMinutes – minute setting
  • setSeconds – установка секунд
  • setMilliseconds – setting milliseconds

On example:

    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

    var myDate = new Date();
    myDate.setDate(15);
    myDate.setMonth(6);
    myDate.setYear(2013);

    var fullDate = myDate.getDate() + " " + months[myDate.getMonth()] +
        " " + myDate.getFullYear() + ", " + days[myDate.getDay()];
    document.write(fullDate); //15 July 2013, Monday 

When setting values, we can pass in a value greater than the maximum allowed value. For example, set the hour to 54:

    myDate.setHour(54);

In this case, the hour value will be 54 – 24 * 2 = 6, and the remaining hours will be two days (24 * 2), which will add two days to the date

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation