Solved

Formating a datetime in javascript

Posted on 2014-11-28
2
72 Views
Last Modified: 2015-02-03
Hej,

What is the best way to format the current date and time as a string in the
format "yyyy-mm-dd hh:mm:ss"?

Thanks!
0
Comment
Question by:soozh
2 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 250 total points
ID: 40470476
You haven't said if you want the leading 0 for the months or just use 1-9

var d = new Date();
alert(d.getFullYear()+"-"+d.getMonth()+"-"+d.getDay()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());

Open in new window

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 40470905
If you'll reuse the code then a combination of jQuery and the Date.js library makes it super simple:

http://jsbin.com/juvene/1/edit?html,js,console

$(function() {
	var n = new Date();

	console.log(n.toString('yyyy-MM-dd hh:mm:ss'));

});

Open in new window


// outputs: "2014-11-29 07:50:10"
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question