0

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

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • http://momentjs.com/ is your best bet for parsing a known date format however, can you explain why `new Date('13-jan-2016')` isn't working for you? – Phil Oct 05 '16 at 03:00
  • [Where can I find documentation on formatting a date in JavaScript?](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Jonathan Lonowski Oct 05 '16 at 03:09
  • The new date function is only printing this type of date 'Wed Jan 13 2016 00:00:00' but l want all the fields of the date in my database to be all numeric which is the reason why l'm trying to convert it to the this format of date 13-01-2016 or this format 13/01/2016. But l also wanted to do it without using other libraries because l'm still learnig – Evans Munatsa Oct 05 '16 at 03:10
  • `var e = Date.parse('13-jan-2016');` will fail - no exception, just wont be a valid Date – Jaromanda X Oct 05 '16 at 03:10

4 Answers4

1

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')); //
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • `var d = new Date(Date.parse('13-jan-2016'));` - wont work - invalid date – Jaromanda X Oct 05 '16 at 03:11
  • `it works in chrome` good if you use Chrum, but it doesn't work in IE, Edge or Firefox – Jaromanda X Oct 05 '16 at 03:14
  • it works, thanks but l'm not sure if l understand the regular expression, and l also did not know that you can put a function after a regular expression, but thanks its good to learn new stuff – Evans Munatsa Oct 05 '16 at 03:23
  • The regex I've used is extremely basic - you can review what each part of it means at [MDN's main regex page](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions). Passing a function as the second argument of `.replace()` is also [explained at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter). – nnnnnn Oct 05 '16 at 03:31
1

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')
Bob Dust
  • 2,370
  • 1
  • 17
  • 13
0

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()

```

willywonka
  • 117
  • 5
0

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