0

I have to sort a table by a date property, but my date looks like this: "14.01.1970 07:55". How can I show this date in a different format than it is on server side? And how can I sort this type of date?

John Bra
  • 181
  • 1
  • 2
  • 12
  • Do you use Moment.js? – sp00m Apr 12 '18 at 11:54
  • You can first convert it into timestamp and then use the `npm` module `dateformat` for converting date in any format – Saptarshi Dey Apr 12 '18 at 11:55
  • I'll try Moment.js. But I have a question to this. If I'm trying to format dates there are some Invalid dates, because of entry format. I mean that it transforms date from format MMDDYYYY, where MONTH is first and in my example I have DAY first, so I need DDMMYYYY format. – John Bra Apr 12 '18 at 12:31

3 Answers3

0

You can tokenize the string and instantiate a new Date object.

var dateStrings = [ "14.01.1970 07:55", "14.01.1972 07:55", "14.01.1971 07:55" ]

function parseDate(dateString) {
  var tokens = dateString.split(/[\.: ]/g);  
  var date   = parseInt(tokens[0], 10);
  var month  = parseInt(tokens[1], 10) - 1;
  var year   = parseInt(tokens[2], 10);
  var hour   = parseInt(tokens[3], 10);
  var minute = parseInt(tokens[4], 10);

  return new Date(year, month, date, hour, minute);
}

// Zulu time 7 hours + timezone offset
console.log(dateStrings.map(parseDate).sort((a, b) => a > b)) 
.as-console-wrapper { top: 0; max-height: 100% !important; }
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Just include moment.js and it will be easy to format: https://momentjs.com/

For example, wrap your timestamp moment(yourtimestamp, 'DD.MM.YYYY HH:mm') and to format what ever you like, just .format(yourformat)

Like this:

moment('14.01.1970 07:55', 'DD.MM.YYYY HH:mm').format('YYYY MM DD')

You can sort them by creating an array of unix timestamps

moment('12.02.1980 08:55', 'DD.MM.YYYY HH:mm').format('X')

const array = [319186500, 319182900];

array.sort()

If you have lodash, maybe _.sortBy

Shnigi
  • 972
  • 9
  • 19
-1

Here is how I would do in Javascript (Untested Code Alert):

// STEP : 1 sample Input
var input = ["01.01.1970 07:55", "06.06.1970 08:55", "03.03.1970 07:55"];

function convertToDateObj(date_string){
    // 14.01.1970 07:55
    date_string = date_string.split('.');
    // [ 0 => 01, 1 => 14, 2 => 1970 07:55 ]
    temp = date_string[0]; date_string[0] = date_string[1]; date_string[1] = date_string[0];
    // 01/14/1970 07:55
    date_string = date_string.join("/");
    // 1970-01-14T02:25:00.000Z
    return new Date(Date.parse(date_string));
}

// STEP : 2 convert to date Objects
var dateObjects = [];
for(var i=0; i<input.length; i++){
   dateObjects[i] = convertToDateObj(input[i]);
}

// STEP : 3 sort them
dateObjects.sort(function(date1, date2){
 if (date1 < date2) {
    return -1;
 } else if (date1 == date2) {
    return 0;
 } else {
    return 1;
 }
})

// your output:
console.log(dateObjects)
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34
  • The *convertToDateObj* function is somewhat illogical in parsing a string to generate another string that is parsed by the built-in parser. Once you have the string parts, give them directly to the Date constructor. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 12 '18 at 21:12