0

I'm looking for a lightweight way of making the following date format more readable.

04OCT2013

I would ideally like to it output:

4 October 2013

Any help would be really appreciated, thanks!

ghghjk
  • 223
  • 4
  • 11
  • did you try anything? – lelloman Oct 04 '13 at 11:01
  • Everything I tried myself was a bit bulky :/ – ghghjk Oct 04 '13 at 11:01
  • check here for date functios http://www.w3schools.com/jsref/jsref_obj_date.asp – Sunil Verma Oct 04 '13 at 11:02
  • The date is pulled from an array – ghghjk Oct 04 '13 at 11:02
  • It's outputted as text, I do not have control over how it is originally formatted. – ghghjk Oct 04 '13 at 11:04
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2Fparse – NimChimpsky Oct 04 '13 at 11:47
  • @NimChimpsky—`Date.parse('04OCT2013')` => NaN. Note that MDN is a [community wiki](https://developer.mozilla.org/en-US/docs/Project:MDN/Contributing/Join_the_community?menu), it's not a standard nor even official documentation, though it is a useful resource for examples and explanations (though ECMA-262 is nearly always a better first reference for ECMAScript language topics). – RobG Oct 07 '13 at 07:16

6 Answers6

1

You first will need to parse your string, then format the parts of it. You can use some full-fledged libary for this (there are a few), or write the two simple functions yourself:

var shortMonths = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dez"],
    longMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function parse(datestring) {
    var date = parseInt(datestring.slice(0,2), 10),
        month = shortMonths.indexOf(datestring.slice(2,5).toLowerCase()),
        year = parseInt(datestring.slice(5,9), 10);
    return new Date(year, month, date);
}
function format(date) {
    return [date.getDate(), longMonths[date.getMonth()], date.getFullYear()].join(" ");
}

> format(parse("04OCT2013"))
"4 October 2013"
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • It might be more "lightweight" to replace `parseInt` with `+`. ;-) – RobG Oct 04 '13 at 11:14
  • 1
    @Alex: The code in the question works. Are you sure that `de_date = "04OCT2013";`? Did you try another date? What was the error you got? – Bergi Oct 04 '13 at 11:48
1

Inspired by Bergi's answer:

function formatDate(s) {
  s = s.toLowerCase().match(/\d+|\D+/g);
  var months = {jan:'January', feb:'February', mar:'March', apr:'April',
                may:'May', jun:'June', jul:'July', aug:'August', sep:'September',
                oct:'October', nov:'November', dec:'December'};
  return +s[0] + ' ' + months[s[1]] + ' ' + s[2]; 
}

formatDate('04OCT2013'); // 4 October 2013
formatDate('14NOV2015'); // 14 November 2015
RobG
  • 142,382
  • 31
  • 172
  • 209
0

You can try with moment.js

<script src="//cdn.jsdelivr.net/momentjs/2.0.0/moment.min.js"></script>

var date = new Date("04OCT2013");
var newDate = moment(date).format("DD MMMM YYYY");

alert(newDate);

Js Fiddle Demo

Sachin
  • 40,216
  • 7
  • 90
  • 102
0
 var months= [ "January", "February", "March", "April", "May", "June", "July", "August",  "September", "October", "November", "December" ];
 var d = new Date();
 var desc = d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear();

But I would suggest date.js or even better moment.js.

George Mavritsakis
  • 6,829
  • 2
  • 35
  • 42
0
var now = new Date();
var monthNamesL = [ "January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December" ],
    monthNamesS = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

var dateFmtd = monthNamesL[now.getMonth()] + ' ' + now.getDay() + ', ' + now.getFullYear();
alert(dateFmtd);
  • The OP wants the much more common date month year order, not the US–centric month date year order. – RobG Oct 07 '13 at 07:08
0

Jsut format the string a bit and use the built in Date type with parse :

var myString = "04OCT2013";

then substring out the first characters, and the last 4 and the month so you can use the built in parser like below :

Date.parse("Aug 9, 1995");

No need to create your own arrays of months.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • The OP seems to want to reformat the string. Or is a Date object required? Anyhow, relying on the Date function to correctly parse a non–standard string is unreliable and can be expected to fail in at least some browsers. – RobG Oct 04 '13 at 11:54
  • @RobG I aqm suggesting you format the string, and then use the parser – NimChimpsky Oct 04 '13 at 11:58
  • I understand your approach. However, the OP seems to want a formatted string, your answer will return either an ECMAScript time value or throw an error, depending on the implementation. Also, there is no [date type](http://ecma-international.org/ecma-262/5.1/#sec-8), though there is a native Date object that can be called as a function or constructor. – RobG Oct 07 '13 at 07:06