× Contact
Home ☰ Menu

The easiest way to generate date and time with JavaScript

2017. 10. 20. 12:31

JavaScript

Date

Time

Function

I made a little script to generate date and time with JavaScript. It's very simple and you can easily customize it.

First we generate the Date object.

var ctime = new Date(); // Generate the Date()

After this, we have to get the necessary parts of it. At some parts we need to make some changes to get a nice result. If the variable is one character long, we add an extra 0 to it. Here is the Year block:

var month = ctime.getMonth() + 1;
if (month < 10){
	month = "0" + month; // If the variable is one character long, we add an extra 0 to it.
}

 We need to repeat it at the Days, Hours, Minutes and Seconds.

Finally, we have to append the variables. You can easily change the format, by modifying the separating characters. Lastly, convert the return variable to String.

// We should append the variables, to get a full date and time.
var now_date = year + '. ' + month + '. ' + day + '. ' + hour + ':' + min + ':' + sec;

return String(now_date); // The functions returns with the date and time. We should convert it to String.

Here is the whole source code: 


function now_date(){
	var ctime = new Date(); // Generate the Date()
	
	var year = ctime.getFullYear();
	
	var month = ctime.getMonth() + 1;
	if (month < 10){
		month = "0" + month; // If the variable is one character long, we add an extra 0 to it.
	}
	
	var day = ctime.getDate();
	if (day < 10){
		day = "0" + day;
	}
	var hour = ctime.getHours();
	if (hour < 10){
		hour = "0" + hour;
	}
	
	var min = ctime.getMinutes();
	if (min < 10){
		min = "0" + min;
	}
	
	var sec = ctime.getSeconds();
	if (sec < 10){
		sec = "0" + sec;
	}
	
	// We should append the variables, to get a full date and time.
	var now_date = year + '. ' + month + '. ' + day + '. ' + hour + ':' + min + ':' + sec;

	return String(now_date); // The functions returns with the date and time. We should convert it to String.
}

Peter Sipos

I am a Web developer. I am developing in web languages like: PHP, HTML, JavaScript, jQuery, NodeJS. In the following posts you can learn some useful techniques.

6 articles available

More articles

  • How to change server time on Ubuntu 14.04

    It's a very simple but useful way to change the server default timezone by location if you use Ubuntu 14.

    Peter Sipos

    2018. 01. 24. 10:33

    Linux

    Ubuntu 14.04

    Timezone

    Configuration

  • Error: Vesta update failed

    If you use VestaCP you may got an update error in email messages. Here is the solutions to fix this problem.

    Peter Sipos

    2017. 10. 27. 19:35

    Vesta

    Update

    Failed

    Error

    Vesta-nginx

    Vesta-php

    Fix

    Solution

    Console

  • Real time location sharing with Node.js and Socket.IO on Google Maps

    In this tutorial I will show the easiest way to retrieve users location from their browser using HTML5 Geolocation API. I will also show you a secondary way to get the user location through IP address. This is useful if users denied the HTML5 Geolocation requests.

    Peter Sipos

    2017. 10. 20. 13:13

    Node.js

    Socket.IO

    Express

    JavaScript

    JQuery

    AJAX

    HTML

    CSS

  • Real Time Chat Application with Node.js and Socket.IO and Bootstrap - Beta

    In this article I'm gonna show my new Node.js Chat application for you. It's under development and it's in BETA version, but you can try and test it.

    Peter Sipos

    2017. 10. 20. 12:02

    Node.js

    Socket.IO

    Express

    JavaScript

    JQuery

    AJAX

    HTML

    CSS

    Bootstrap

  • Monty Hall problem

    You are in a show. There are three doors and you have to select one door. Behind one door there is the prize: the car. Behind the two other doors you can only find a goat. The mission is simple: find the door which hides the car.

    Peter Sipos

    2017. 10. 18. 20:28

    PHP

    HTML