1

I am a newbie and want to convert 2014-09-25T09:54:00 into 9:54 AM Thu, 25th Sep..

can someone please suggest me a way to do it in javascript.

Thanks

  • please check http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript?rq=1 – rahul_send89 Sep 23 '14 at 09:58

3 Answers3

0

As Phrogz said here:

  1. Parse the string into a JavaScript Date object.
  2. Format the Date object however you like.

See also: Why does Date.parse give incorrect results? and How to format a JavaScript date questions.

Example:

var myDate = new Date("2014-09-25T09:54:00")
var myString = myDate.customFormat("#h#:#mm# #AMPM# #DDD#, #D##th# #MMM#");

See this code in action here

Community
  • 1
  • 1
Lomanic
  • 371
  • 4
  • 15
0
here is a great JavaScript library that handles this very well, and only 5.5kb minified.

http://momentjs.com/

It looks something like this:


moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am
moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM"
You can also pass in dates as a String with a format, or a Date object.

var date = new Date();
moment(date); // same as calling moment() with no args

// Passing in a string date
moment("12-25-1995", "MM-DD-YYYY");
Also has great support for languages other then English, like, Russian, Japanese, Arabic, Spanish, etc..
Amit Guleria
  • 211
  • 2
  • 11
0

I made an fiddle for your case.

It is based on this awesome tutorial here which makes formatting dates very easy.

Javascript Code:

var d_names = new Array("Sun", "Mon", "Tue",
"Wed", "Thu", "Fri", "Sat");

var m_names = new Array("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", "Aug", "Sept", 
"Oct", "Nov", "Dec");

var d = new Date();
var curr_date = d.getDate();
var sup = "";
if (curr_date == 1 || curr_date == 21 || curr_date ==31)
   {
   sup = "st";
   }
else if (curr_date == 2 || curr_date == 22)
   {
   sup = "nd";
   }
else if (curr_date == 3 || curr_date == 23)
   {
   sup = "rd";
   }
else
   {
   sup = "th";
   }

var a_p = "";

var curr_hour = d.getHours();

if (curr_hour < 12)
   {
   a_p = "AM";
   }
else
   {
   a_p = "PM";
   }
if (curr_hour == 0)
   {
   curr_hour = 12;
   }
if (curr_hour > 12)
   {
   curr_hour = curr_hour - 12;
   }

var curr_day = d.getDay();
var curr_min = d.getMinutes();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var curr_hour = d.getHours();
alert((curr_hour + " : " + curr_min + " " + a_p)+ (" " + d_names[curr_day] + ", " + curr_date + sup + " "  
+ m_names[curr_month]  /* +" " + curr_year */));
Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38