Titanium - Get and Display time


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var time = Ti.UI.createLabel({
  2. text:'',
  3. font:{
  4. fontFamily:'Verdana',
  5. fontWeight:'bold',
  6. fontSize:14
  7. },
  8. color:'#fff',
  9. shadowColor:'#333',
  10. shadowOffset:{x:1,y:1},
  11. textAlign:'right',
  12. width:Ti.Platform.displayCaps.platformWidth,
  13. height:20,
  14. top:45,
  15. left:-13
  16. });
  17.  
  18. function getFormattedTime()
  19. {
  20. var amPM = '';
  21. var d = new Date();
  22. var currentHour = d.getHours();
  23.  
  24. if (currentHour < 12)
  25. {
  26. amPM = 'AM';
  27. }
  28. else
  29. {
  30. amPM = 'PM';
  31. }
  32.  
  33. if (currentHour == 0)
  34. {
  35. currentHour = 12;
  36. }
  37.  
  38. if (currentHour > 12)
  39. {
  40. currentHour = currentHour - 12;
  41. }
  42.  
  43. var currentMinute = d.getMinutes();
  44. currentMinute = currentMinute + '';
  45.  
  46. if (currentMinute.length == 1)
  47. {
  48. currentMinute = '0' + currentMinute;
  49. }
  50. time.text = currentHour + ':' + currentMinute + ' ' + amPM;
  51. }
  52. var clockInterval = setInterval(getFormattedTime,5000);
  53. getFormattedTime();
  54. win.add(time);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.