Evans Munatsa
Evans Munatsa

Reputation: 9

converting string dates in javascript

hey guys l've been working with CSV files and some of them contain dates and now l want to store them in the database but there is a problem l'm facing.

l have a date like this: 10-Feb-2016 and l it to be like this: 10-02-2016

but so far l've been trying to use the date.parse function of which is giving some different things here is my code that l have tried to use

var d = '13-jan-2016';
var e = Date.parse('13-jan-2016')
var m = new Date( e).toISOString()
console.log(m)

l have also tried to use the split function but there is still some digit that l don't understand of why they are there or its my lack of understanding when it come to the parse() function in javascript

Upvotes: 0

Views: 576

Answers (4)

Prajyot Khandeparkar
Prajyot Khandeparkar

Reputation: 49

If expected input string format is same '13-Jan-2016' Here is an example

var dateString = '13-Dec-2015';
var date = new Date(dateString);
var month = date.getMonth() + 1;
month = ('0' + month).slice(-2);
var dateArray = dateString.split('-');
date = dateArray[0]+'-'+month+'-'+dateArray[2];
console.log(date); //13-12-2016

Upvotes: 0

Bob Dust
Bob Dust

Reputation: 2460

I'd like to suggest a handy 3rd-party http://momentjs.com/docs/#/parsing/string-format and with that library it can be done like this:

moment('13-jan-2016', 'DD-MMM-YYYY').format('DD-MM-YYYY')

Upvotes: 1

nnnnnn
nnnnnn

Reputation: 150070

You can use a date library, like the much-loved MomentJS, or perhaps just write a simple function or two to handle it yourself, something like this:

function zeroPad(v) {
  return v >= 10 ? v : "0" + v;
}

function formatDate(s) {
  var d = new Date(Date.parse('13-jan-2016'));
  return zeroPad(d.getDate()) + "-" + zeroPad(d.getMonth() + 1) + "-" + d.getFullYear();
}

console.log(formatDate('13-jan-2016')); // '13-01-2016'

...except that as Jaromanda X pointed out Date.parse() won't handle that particular format reliably in some (most) browsers (though it worked fine for me in Chrome). Given you don't seem to be actually using the date as a date, you are just transforming it from one known format to another, you could do something like the following, which will work even in older browsers like IE<9 that don't support Date.parse() at all:

function formatDate(s) {
  var months = { 'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05', 'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11', 'dec': '12' };
  return s.replace(/[a-z]{3}/i, function(m) { return months[m.toLowerCase()]; });
}

console.log(formatDate('13-jan-2016')); //
console.log(formatDate('02-apr-2016')); //
console.log(formatDate('25-DEC-2016')); //

Upvotes: 1

willywonka
willywonka

Reputation: 117

What you're looking for is not really an ISO string. If you don't want to use a library, I think this is your best bet. Source for months: http://www.w3schools.com/jsref/jsref_getmonth.asp ```

var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var date = new Date('13-jan-2016')
var str = date.getDate() + '-' + month[date.getMonth()] + '-' + date.getFullYear()

```

Upvotes: 0

Related Questions