Convert date to specific format in javascript?

Hi, I want to convert the current date to a format as indicated in javascript: April 12, 2011 06:42:03.

Any suggestions?????

+5
source share
4 answers

A small example that I just put together for you. It’s easy to say what’s going on.

var monthNames = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");

var today = new Date();
var cDate = today.getDate();
var cMonth = today.getMonth();
var cYear = today.getFullYear();

var cHour = today.getHours();
var cMin = today.getMinutes();
var cSec = today.getSeconds();

alert( monthNames[cMonth] + " " +cDate  + "," +cYear + " " +cHour+ ":" + cMin+ ":" +cSec );
+17
source

You viewed this

dateFormat(now, "mmm dd, yyyy hh:MM:ss");

it is lightweight (1.2 KB) and supports the following formats

  • d Day of the month in numbers; without a leading zero for single days.
  • dd Day of the month in numbers; leading zero for single days.
  • ddd Day of the week as a three-letter abbreviation.
  • dddd Day of the week as a full name.
  • m Month in the form of numbers; without a leading zero for single months.
  • ; .
  • mmm .
  • mmmm .
  • yy ; 10.
  • yyyy .
  • h ; (12- ).
  • hh ; (12- ).
  • H ; (24- ).
  • HH; (24- ).
  • M ; . Uppercase M CF timeFormat m, .
  • MM Minutes; . CF timeFormat , .
  • s ; .
  • ss Seconds; .
  • l L . 3 . L 2 .
  • t : a p.
  • tt : am pm.
  • T , : A P. T CF t, .
  • TT : AM PM. Uppercase TT CF tt, .
  • Z , . EST MDT. Opera GMT ​​/UTC, . GMT-0500
  • o GMT/UTC , . -0500 +0230.
  • S (st, nd, rd th). d.
  • '...' "..." . .
  • UTC: . UTC/GMT/Zulu, . "UTC:" .
+5

All Articles