Question

In BIRT, i have a column containing a datetime stored as a string. I need to convert these string to datetime format and put the result in another column using Javascript. The string is the form of: for example: Fri 21 Feb 2014, 09:40 AM.

Hence this when converted to a datetime format and exported to excel, the column should be treat as a date. Can any one of you help me to do it?

Cheers,

Was it helpful?

Solution

Other answers do not take into consideration this question is in a BIRT context.

  • Create a computed column in your dataset, with "Date time" as datatype
  • Enter as expression:

new Date(row["myDateStringField"]);

Where "myDateStringField" is your DateTime column in a String format. Then use this computed column in your report instead of the String column.

That's it!

OTHER TIPS

Checkout momentjs!

You can parse your time of any format like

moment("12-25-1995", "MM-DD-YYYY");

In your case, you don't even have to specify the format. It automatically recognizes it.

And you can output ISO format or convert it to a Javascript Date object.

This is extremely easy to do with javascript. The following code will make a date in a format that Excel will recognize as a date.

http://jsfiddle.net/bbankes/d7SwQ/

var dateString = 'Fri 21 Feb 2014, 09:40 AM';
var date = new Date(dateString);
var yr = date.getFullYear();
var mo = date.getMonth() + 1;
var day = date.getDate();

var hours = date.getHours();
var hr = hours < 10 ? '0' + hours : hours;

var minutes = date.getMinutes();
var min = (minutes < 10) ? '0' + minutes : minutes;

var seconds = date.getSeconds();
var sec = (seconds < 10) ? '0' + seconds : seconds;

var newDateString = yr + '-' + mo  + '-' + day;
var newTimeString = hr + ':' + min + ':' + sec;

var excelDateString = newDateString + ' ' + newTimeString;

If you just want to reformat 'Fri 21 Feb 2014, 09:04 AM' as '2014-02-21 09:04', then the following will do:

function stringToTimestamp(s) {
  var t = s.match(/[\d\w]+/g);
  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'};
  function pad(n){return (n<10?'0':'') + +n;}
  var hrs = t[4] % 12;
  hrs += /pm$/i.test(t[6])? 12 : 0;

  return t[3] + '-' + months[t[2].toLowerCase()] + '-' + pad(t[1]) + ' ' +
         pad(hrs) + ':' + pad(t[5]);
}

console.log(stringToTimestamp('Fri 21 Feb 2014, 09:04 AM')); // 2014-02-21 09:04

use the ISO format: YYYY-MM-DDTHH:MM:SS or YYYY-MM-DD

new Date('2011-04-11T11:51:00');

or

new Date('2011-04-11');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top