Posted By

shaundunne on 02/27/11


Tagged

javascript iphone Appcelerator Titnium Andriod


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Titanium - Get and Display time


 / Published in: JavaScript
 

  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  

You need to login to post a comment.