Hot File

Get Current Date and Time in Javascript

View: 214    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: none

10 point/1 review File has been tested

How to get current date and time in Javascript. How to display current date and time in Javascript.

Introduction

How to get current date and time in Javascript. How to display current date and time in Javascript.

In javascript date object is used to get current date and time. Using javascript date object you can read the current date and time of user system.

Let’s check how to get current date time in javascript.

How to Get Current Date and Time in Javascript

Let’s first create a date object.

/* Create date object. */
 
var date = new Date(); 
 
/* Alert date object value. */
 
 alert(date);
 
/* Check the value in console. */
 
 console.log(date);
 
// It will Print - Sat Oct 24 2015 08:17:39 GMT+0530 (India Standard Time)

How to Get Current Timestamp

We already created date object. Now to get current timestamp (in milliseconds), Javascript providesgetTime() method.

getTime() return the timestamp value in milliseconds as compare to other programming language such as PHP which returns the timestamp value in seconds.

/* get current timestamp. */
 
var currentTimestamp = date.getTime();
 
/* Alert the value of current timestamp. */
 
alert(currentTimestamp);
 
/* To display in console. */
 
console.log(currentTimestamp);

Print Current Year

To print current year in Javascript, javascript provides getFullYear() method.

var currentYear = date.getFullYear();
 
alert(currentYear);
console.log(currentYear);

getMonth() Method in Javascript – Display Current Month in Numeric

getMonth() function return numeric value of month in the range of 0 to 11.
It starts from 0, so to get the current month we need to add plus one.

var currentMonth = date.getMonth() + 1;
 
console.log(currentMonth);

How to Display Current Date in Javascript

We already discussed how to create date object and it’s useful methods.

var date = new Date();
      currentDate = date.getDate();     // Get current date
      month       = date.getMonth() + 1; // current month
      year        = date.getFullYear();
 
alert("current date is " + currentDate + "/" + month + "/" + year);
 
console.log("current date is " + currentDate + "/" + month + "/" + year);

How to Display Current Time in Javascript

To display current time in javascript let’s first create a object

var currentTime = new Date();

getHours() = It displays the current hour.

getMinutes() = Return current minute.

getSeconds() = Return the value of second.

var currentTime = new Date();
    hour = currentTime.getHours();
    min  = currentTime.getMinutes();
    sec  = currentTime.getSeconds();
 
alert("current Time is" + hour + ":" + min + ":" + sec);
 
console.log("current Time is " + hour + ":" + min + ":" + sec);

List of Methods for Date Object

I discuss only few methods, here is a complete list of methods which you can use with date object

getDate: Returns the day of the month (1-31) for the specified date according to local time.
getDay: Returns the day of the week (0-6) for the specified date according to local time.
getFullYear: Returns the year (4 digits for 4-digit years) of the specified date according to local time.
getHours: Returns the hour (0-23) in the specified date according to local time.
getMilliseconds: Returns the milliseconds (0-999) in the specified date according to local time.
getMinutes: Returns the minutes (0-59) in the specified date according to local time.
getMonth: Returns the month (0-11) in the specified date according to local time.
getSeconds: Returns the seconds (0-59) in the specified date according to local time.
getTime: Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).
getTimezoneOffset: Returns the time-zone offset in minutes for the current locale.
getUTCDate: Returns the day (date) of the month (1-31) in the specified date according to universal time.
getUTCDay: Returns the day of the week (0-6) in the specified date according to universal time.
getUTCFullYear: Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
getUTCHours: Returns the hours (0-23) in the specified date according to universal time.
getUTCMilliseconds: Returns the milliseconds (0-999) in the specified date according to universal time.

getUTCSeconds: Returns the seconds (0-59) in the specified date according to universal time.
getYear: Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear instead.
setDate: Sets the day of the month (1-31) for a specified date according to local time.
setFullYear: Sets the full year (4 digits for 4-digit years) for a specified date according to local time.
setHours: Sets the hours (0-23) for a specified date according to local time.
setMilliseconds: Sets the milliseconds (0-999) for a specified date according to local time.
setMinutes: Sets the minutes (0-59) for a specified date according to local time.
setMonth: Sets the month (0-11) for a specified date according to local time.
setSeconds: Sets the seconds (0-59) for a specified date according to local time.
setTime: Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC, allowing for negative numbers for times prior.
setUTCDate: Sets the day of the month (1-31) for a specified date according to universal time.
setUTCFullYear: Sets the full year (4 digits for 4-digit years) for a specified date according to universal time.
setUTCHours: Sets the hour (0-23) for a specified date according to universal time.
setUTCMilliseconds: Sets the milliseconds (0-999) for a specified date according to universal time.
setUTCMinutes: Sets the minutes (0-59) for a specified date according to universal time.
setUTCMonth: Sets the month (0-11) for a specified date according to universal time.
setUTCSeconds: Sets the seconds (0-59) for a specified date according to universal time.
setYear: Sets the year (usually 2-3 digits) for a specified date according to local time. Use setFullYear instead.
toDateString: Returns the “date” portion of the Date as a human-readable string.

Get Current Date and Time in Javascript

Get Current Date and Time in Javascript Posted on 21-09-2016  How to get current date and time in Javascript. How to display current date and time in Javascript. 10/10 214

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 471    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    4.5/1 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 542    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    4.5/1 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 248    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 255    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 263    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    4.5/2 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 237    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 4859    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 641    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    0/0 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd