comparing todays date with date from sqlite db

You must Login before you can answer or comment on any questions.

what i want to do is compare todays date using var date=new Date(); to a date ive set in an sqlite db

basically eg:

var date=new Date();
 
if(date >= win.summerstartsdate)
 
{ // summer has started}
but i'm not sure how i would structure the date in the db eg 01:06:2012 or 20120601 etc so that it would match the output from new Date and be able to be compared by a greater than or less than operator

i'm not sure where to start

— asked 8 months ago by adrian harris
2 Comments
  • can you post the format of date that comes from your sqlite DB?

    — commented 8 months ago by Sarafaraz Babi

  • the point is i dont know what the best format is to use in the db :)

    — commented 8 months ago by adrian harris

3 Answers

this works perfectly for me

newdate ='2011-05-04'? 
// newdate is the result of a db query
 
var date = new Date();
date = date.getTime();
date = date/1000;
 
// date = 1351002562
 
var newdate = strtotime(newdate);  // returns earller
 
//newdate = 1304460000
 
if(date >= newdate)
{ 
alert('later');
//do stuff
}
 
if(date <= newdate)
{ 
alert('earlier');
//do stuff
}

Hello Adrian,

you can set current date formate like....

var date = new Date();
date = dateFormat(date, "dd:mm:yyyy");

— answered 8 months ago by Ritesh .
answer permalink
6 Comments
  • what happen Adrian? Is it not working for you?

    — commented 8 months ago by Ritesh .

  • initially it did but then i ran into comparing today with something in 2013 and didnt work - just playing around with it again now - brb... :)

    — commented 8 months ago by adrian harris

  • adrian, i think you need only date format as you say in question comment so i give only date format demo... anyway best luck...

    — commented 8 months ago by Ritesh .

  • Show 3 more comments

var win = Titanium.UI.createWindow({

title:'Date Comparison',

backgroundColor:'#fff'

});

var db = Titanium.Database.open('mydb');

db.execute('CREATE TABLE IF NOT EXISTS date_table (ID INTEGER, dbdate date)');

db.execute('INSERT INTO date_table (ID, dbdate ) VALUES(?,?)',1,'April 25, 2005');

db.execute('INSERT INTO date_table (ID, dbdate ) VALUES(?,?)',2,'December 25, 2005');

var rows = db.execute('SELECT * FROM date_table');

var dat1= db.execute('SELECT dbdate FROM date_table where ID=1').toString();

var dat2= db.execute('SELECT dbdate FROM date_table where ID=2').toString();

if (dat2>=dat1 ) {

Ti.API.info('its Summer');

} else{

Ti.API.info('Bug In Titanium Dates');

};

win.open();

— answered 7 months ago by Muhammad Adnan
answer permalink
1 Comment
  • If you want comparison with todays DATE then use

    var dat1 = new Date().toString();

    var dat2=db.execute('INSERT INTO date_table (ID, dbdate ) VALUES(?,?)',1,'2012-11-24 05:22:44 +0000');

    — commented 7 months ago by Muhammad Adnan

Your Answer

Think you can help? Login to answer this question!