Solved

Formating a datetime in javascript

Posted on 2014-11-28
2
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 43

Accepted Solution

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

710 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