This shows you the differences between two versions of the page.
sample_code:datediff [2016/07/23 08:17] stevegarman tidy |
sample_code:datediff [2016/07/29 13:39] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Date Difference demo ====== | ||
- | This demo uses the UIExtras plugin, which is not free. | ||
- | You can buy it outright or you can use it for free if you have a Premium subscription. | ||
- | |||
- | ===== What can I learn from this sample? ===== | ||
- | |||
- | The main subjects explored in this demo are | ||
- | * Changing layout when the screen rotates | ||
- | * Calculating the difference between two dates in years, months and days. | ||
- | |||
- | ===== What if I don't have the plugin? ===== | ||
- | Of course you could always buy it or take out a Premium subscription. | ||
- | |||
- | Alternatively, | ||
- | |||
- | Or you can remove the UIExtras stuff and replace it with your own controls (TextEdits? | ||
- | |||
- | ===== The code ===== | ||
- | <code JavaScript dateDiff.js> | ||
- | // DroidScript | ||
- | // needs UIExtras plugin to run without modification | ||
- | // | ||
- | // tries to handle screen rotation helpfully | ||
- | // by placing controls vertically or horizontally | ||
- | // as appropriate. This does not rely on the plugin | ||
- | |||
- | // the dateDiff function does not rely on the plugin | ||
- | |||
- | app.LoadPlugin( " | ||
- | var date1, | ||
- | var picker, | ||
- | |||
- | function OnStart() | ||
- | { | ||
- | | ||
- | var orient = app.GetOrientation()==" | ||
- | lay = app.CreateLayout( " | ||
- | var lay1 = app.CreateLayout( " | ||
- | var lay2 = app.CreateLayout( " | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | var uix = app.CreateUIExtras(); | ||
- | |||
- | | ||
- | | ||
- | | ||
- | |||
- | txt= app.CreateText( "", | ||
- | | ||
- | | ||
- | | ||
- | |||
- | | ||
- | | ||
- | | ||
- | |||
- | txt2= app.CreateText( "", | ||
- | | ||
- | | ||
- | |||
- | | ||
- | | ||
- | } | ||
- | function txt_OnTouch() | ||
- | { | ||
- | if(confirm(" | ||
- | } | ||
- | |||
- | function init() | ||
- | { | ||
- | var date = new Date(); | ||
- | date1 = new Date(date.getFullYear(), | ||
- | // | ||
- | date2 = new Date(2019, mth.jan, 1); | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | |||
- | function OnDateChanged( year, month, day ) | ||
- | { | ||
- | date1 = new Date( year, month, day); | ||
- | | ||
- | } | ||
- | function OnDate2Changed( year, month, day ) | ||
- | { | ||
- | date2 = new Date( year, month, day); | ||
- | | ||
- | } | ||
- | |||
- | // something changed, update text. | ||
- | function showDiff() | ||
- | { | ||
- | var d = 1000*3600*24; | ||
- | var diff = Math.round((date2.getTime() - date1.getTime())/ | ||
- | txt.SetText( diff + " days" ); | ||
- | txt.SetTextSize( 32 ); // may have been undone by autoscale | ||
- | |||
- | var diffObj = dateDiff(date1, | ||
- | var s = diffObj.sign; | ||
- | if(diffObj.years > 0) s += diffObj.years+" | ||
- | if(diffObj.months > 0) s += diffObj.months+" | ||
- | s += diffObj.days+" | ||
- | txt2.SetText( s ); | ||
- | txt2.SetTextSize( 32 );// may have been undone by autoscale | ||
- | } | ||
- | |||
- | // lazy way to choose month when using Date object | ||
- | function setMth(m) | ||
- | { | ||
- | m.jan=0; | ||
- | m.feb=1; | ||
- | m.mar=2; | ||
- | m.apr=3; | ||
- | m.may=4; | ||
- | m.jun=5; | ||
- | m.jul=6; | ||
- | m.aug=7; | ||
- | m.sep=8; | ||
- | m.oct=9; | ||
- | m.nov=10; | ||
- | m.dec=11; | ||
- | } | ||
- | |||
- | // screen orientation has probably changed | ||
- | function OnConfig() | ||
- | { | ||
- | if(app.GetOrientation()==" | ||
- | lay.SetOrientation( " | ||
- | else lay.SetOrientation( " | ||
- | //let autoscale do its job again | ||
- | txt.SetTextSize( 32 ); | ||
- | txt2.SetTextSize( 32 ); | ||
- | } | ||
- | | ||
- | // calculate the absolute difference in days, months and years between 2 days | ||
- | //ignoring any time component | ||
- | function dateDiff(dt1, | ||
- | { | ||
- | // setup return object | ||
- | var ret = {days:0, months:0, years: | ||
- | |||
- | // if same date, nothing to do | ||
- | if (dt1 == dt2) return ret; | ||
- | |||
- | // we will do absolute diff and set the sign | ||
- | if (dt1 > dt2) | ||
- | { | ||
- | var dtmp = dt2; | ||
- | dt2 = dt1; | ||
- | dt1 = dtmp; | ||
- | ret.sign = " | ||
- | } | ||
- | |||
- | // populate variables for comparison | ||
- | var year1 = dt1.getFullYear(); | ||
- | var year2 = dt2.getFullYear(); | ||
- | |||
- | var month1 = dt1.getMonth(); | ||
- | var month2 = dt2.getMonth(); | ||
- | |||
- | var day1 = dt1.getDate(); | ||
- | var day2 = dt2.getDate(); | ||
- | |||
- | // calculate differences | ||
- | ret[' | ||
- | ret[' | ||
- | ret[' | ||
- | |||
- | // cope with any negative values. | ||
- | if (ret[' | ||
- | { | ||
- | // can't span months by arithmetic, use temp date | ||
- | var dtmp = new Date(dt1.getFullYear(), | ||
- | |||
- | var numDays = dtmp.getDate(); | ||
- | |||
- | ret[' | ||
- | ret[' | ||
- | |||
- | } | ||
- | |||
- | // months is pure arithmetic | ||
- | if (ret[' | ||
- | { | ||
- | ret[' | ||
- | ret[' | ||
- | } | ||
- | |||
- | return ret; | ||
- | } | ||
- | </ |