Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formating a datetime in javascript

Posted on 2014-11-28
2
Medium Priority
?
78 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 1000 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 43

Accepted Solution

by:
Rob earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

971 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