Question

input is 1303 the output must be March 2013 Is it possible in javascript? i've tried looking for anything especially moment.js but it returns 1980

here is the jsfiddle http://jsfiddle.net/4eG7q/

Était-ce utile?

La solution

var input = "1303";
var date = moment(input, 'YYMM').format("YYYY - MMMM");

Fiddle

Autres conseils

Assuming that 1303 means the 13th year after 2000 and the 03 month in that year, you can dissect it and turn it into a string in plain javascript like this:

var input = 1303;
var year = Math.floor(input / 100);
var month = input % 100;

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var output = months[month - 1] + " " + (2000 + year);

Working Demo: http://jsfiddle.net/jfriend00/Ve4VH/

new Date('1303'
.replace(/(\d{2})(\d{2})/, '$2/01/20$1'))
.toString()
.replace(/^.*? (.*?) .*? (.*?) .*?$/, '$1 $2');

This will give you Mar 2012, instead of March 2013 but it's cheaper :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top