PDA

View Full Version : Convert data to mm:ss



C100
28 Mar 2010, 5:28 AM
Hi

I am very new to Javascript and ExtJS, so sorry if this is a bit basic.

I have data coming from a database as Duration = 193985
I would like to change it into mm: ss

I have started with something like, but it doesnt quite work

-:

{
header: 'Duration',
readOnly: true,
dataIndex: 'duration',
hidden: false,
rerender: function secs2strPad(num) {
num = num + "";
while(num.length < 2)
num = "0" + num;
return num;

function secs2str(sec) {
var hours = secs2strPad(Math.floor(sec/3600));
var minutes = secs2strPad(Math.floor((sec%3600)/60));
var seconds = secs2strPad((sec%3600)%60);
return hours + ":" + minutes + ":" + seconds;
}}

Animal
28 Mar 2010, 12:36 PM
Create your own date formatting function.

http://www.extjs.com/deploy/dev/docs/?class=Date&member=formatFunctions.

Eg, I have to create a duration, but in hours, minutes and seconds so I have two formats for the client



(function() {
var oneHour = 1000 * 60 * 60,
oneDay = oneHour * 24;

// Days + h:m:s
Date.formatFunctions.duration = function() {
var d, m = this.getTime();
m -= (m % oneDay);
return ((d = m / oneDay) ? (d + ' ') : '') + this.format('H:i:s');
};

// h:m:s where h can be greater than 24
Date.formatFunctions.durationHMS = function() {
var d, m = this.getTime();
if (m < 0) {
return '&#x221e;'; // -ve duration. Use infinity symbol to signify error.
}
m -= (m % oneHour);
return (m / oneHour) + ':' + this.format('i:s');
};
})();

C100
28 Mar 2010, 2:16 PM
Hi

Thanks for the help.

Ive not really got to creating new functions (Im still working my way through the tutorials, where in my code would I put it ?

var CurrentDataStore;
var CurrentColumnModel;
var CurrentListingEditorGrid;
var CurrentListingWindow;
var CurrentTime = new Date();

Ext.onReady(function(){

Ext.QuickTips.init();

CurrentDataStore = new Ext.data.Store({
id: 'CurrentDataStore',
proxy: new Ext.data.HttpProxy({
url: 'database.php',
//method: 'POST'
}),
baseParams:{task: "LISTING"}, // this parameter is passed for any HTTP request
reader: new Ext.data.JsonReader({
root: 'results',
totalProperty: 'total',
id: 'id'
},[
{name: 'artist', type: 'string', mapping: 'artist'},
{name: 'title', type: 'string', mapping: 'title'},
{name: 'album', type: 'string', mapping: 'album'},
{name: 'duration', type: 'int', mapping: 'duration'},
//{name: 'starttime', type: 'date', mapping: 'starttime'},

]),

});


CurrentColumnModel = new Ext.grid.ColumnModel(
[{

header: 'Artist',
dataIndex: 'artist',
width: 250,
editor: new Ext.form.TextField({
allowBlank: false,
//maxLength: 120,
maskRe: /([a-zA-Z0-9\s]+)$/
})
},{
header: 'Title',
dataIndex: 'title',
width: 250,
editor: new Ext.form.TextField({
allowBlank: false,
//maxLength: 100,
maskRe: /([a-zA-Z0-9\s]+)$/
})
},{
header: 'Album',
dataIndex: 'album',
width: 300,
editor: new Ext.form.TextField({
allowBlank: false,
//maxLength: 100,
maskRe: /([a-zA-Z0-9\s]+)$/
})
},

{
header: 'Duration',
readOnly: true,
width: 50,
dataIndex: 'duration',
//width: 50,
hidden: false,
}]);

var task = {
run: function() {
CurrentDataStore.load()
},
interval: 1000*10 //10 seconds
}

var runner = new Ext.util.TaskRunner();

runner.start(task);

CurrentColumnModel.defaultSortable= false;

CurrentListingEditorGrid = new Ext.grid.EditorGridPanel({
id: 'CurrentListingEditorGrid',
store: CurrentDataStore,
cm: CurrentColumnModel,
enableColLock:false,
clicksToEdit:1,
selModel: new Ext.grid.RowSelectionModel({singleSelect:false})
});


CurrentListingWindow = new Ext.Window({
id: 'CurrentListingWindow',
title: 'Currently Playing',
closable:false,
width:900,
height:75,
//plain:true,
striperows:true,
autofit:true,
layout: 'fit',
items: CurrentListingEditorGrid
});

CurrentDataStore.load();
CurrentListingWindow.show();

});