Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formating a datetime in javascript

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

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…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

664 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