cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract month from Date

0 Kudos

Hi Experts,

I have trying to extract month from date. Below is my code for my xsjs file:

var conn = $.db.getConnection();
var d = new Date();
//YYYY-MM-DDThh:mm:ss
var output = d.getFullYear() + '-' +
('0' + (d.getMonth()+1)).slice(-2) + '-' +
('0' + d.getDate()).slice(-2) + 'T' +
('0' + d.getHours()).slice(-2) + ':' +
('0' + d.getMinutes()).slice(-2) + ':' +
('0' + d.getSeconds()).slice(-2);

var pstmt = conn.prepareStatement("SELECT \"date1\", \"date2\" FROM \"y\" where \"date1\" >'"+output+"'" );

pstmt.execute();

var a = "TEST";
var rs = pstmt.getResultSet();
if(!rs.next()){
$.response.setBody("got nothing");
}else{
var start=rs.getString(1);
// var month=start.getMonth();
//var month= to_char(to_date(y.date1,'dd-MM-yyyy'),'MM')
//var month=EXTRACT(MONTH FROM y.date1);
// $.response.setBody(rs.getString(1)+ rs.getString(2));
$.response.setBody(month); }

I am always getting the error getMonth() is not a function.
Could you please tell me how can I follow up on this?

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor
0 Kudos

Hi Meghna,

what type has the column containing your date? Is it a String or a Date type?

In your coding (var start = rs.getString(1)) you are assuming it is a String. On a String you cannot call the function "getMonth". If it is really a String you have to first convert your String to a Date (depending on your format).

If the column has a Date type already, you should get the value from the result set using method "getDate" (e.g. var start = rs.getDate(1)). With that you get a Date reference on which you can execute function "getMonth".

Regards,
Florian

PS:

You are using the legacy $.db API. Maybe you can also check to switch to the new $.hdb interface which simplifies everything.

0 Kudos

Hi Florian,

Thank you so much. You are correct. I was using getString(), when I tried to use getDate(), it started to work. This really helped me alot!

Answers (0)