View Full Version : how do i format Date to display in Quarter?
does anybody know how to format DATE to display in QUARTER?
i really appreciate it.
--- a newbie ---
mordred
10-09-2002, 11:45 PM
bgn, please be descriptive and elaborate. Do you want to retrieve the quarter of the year for a given date (which you have as a JavaScript Date object)? If so, give this try a shot:
var quarter = Math.ceil(( new Date().getMonth() + 1) / 3 );
alert(quarter);
Thanks for ur help Mordred... but that's not what im looking for.
I already have an existing SQL string that pulls the Date from database. The result is for example '10/10/02' that displayed on my webpage. Now i want it to display as '3rd Quarter of 2002' instead. Later on if the date is changed in the database, for example '01/01/03', then the new script should automatically convert it to '1st Quarter of 2003'. Hope it's descriptive enough.
glenngv
10-10-2002, 04:34 AM
var dateFromDB = "10/10/02";//insert the server-side variable here
var d = new Date(dateFromDB);
var y = d.getFullYear();
var m = d.getMonth()+1;
if (m<=3) str = "1st Quarter of"+y;
else if (m<=6) str = "2nd Quarter of"+y;
else if (m<=9) str = "3rd Quarter of"+y;
else str = "4th Quarter of"+y;
alert(str);
mordred
10-10-2002, 08:02 AM
I don't know which SQL database you are using, but most have already excellent date formatting functions. Using these is often very convenient, and they also work if the user has JS disabled. In MySQL, you would simply do "QUARTER(date)" to get the number of the quarter of this DATE field. Quite straightforward.
If you still want to do it with JS, try
var dateStr = "10/10/02";
var dateArr = dateStr.split("/");
var quarter = Math.ceil(( new Date("20" + dateArr[2], dateArr[1] - 1, dateArr[0]).getMonth() + 1) / 3 );
alert(quarter);
Thanks for all the help guys!!!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.