uni_android_plugin_project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

  1. function formatTime(time) {
  2. if (typeof time !== 'number' || time < 0) {
  3. return time
  4. }
  5. var hour = parseInt(time / 3600)
  6. time = time % 3600
  7. var minute = parseInt(time / 60)
  8. time = time % 60
  9. var second = time
  10. return ([hour, minute, second]).map(function (n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }).join(':')
  14. }
  15. function formatLocation(longitude, latitude) {
  16. if (typeof longitude === 'string' && typeof latitude === 'string') {
  17. longitude = parseFloat(longitude)
  18. latitude = parseFloat(latitude)
  19. }
  20. longitude = longitude.toFixed(2)
  21. latitude = latitude.toFixed(2)
  22. return {
  23. longitude: longitude.toString().split('.'),
  24. latitude: latitude.toString().split('.')
  25. }
  26. }
  27. var dateUtils = {
  28. UNITS: {
  29. '年': 31557600000,
  30. '月': 2629800000,
  31. '天': 86400000,
  32. '小时': 3600000,
  33. '分钟': 60000,
  34. '秒': 1000
  35. },
  36. humanize: function (milliseconds) {
  37. var humanize = '';
  38. for (var key in this.UNITS) {
  39. if (milliseconds >= this.UNITS[key]) {
  40. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  41. break;
  42. }
  43. }
  44. return humanize || '刚刚';
  45. },
  46. format: function (dateStr) {
  47. var date = this.parse(dateStr)
  48. var diff = Date.now() - date.getTime();
  49. if (diff < this.UNITS['天']) {
  50. return this.humanize(diff);
  51. }
  52. var _format = function (number) {
  53. return (number < 10 ? ('0' + number) : number);
  54. };
  55. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
  56. _format(date.getHours()) + ':' + _format(date.getMinutes());
  57. },
  58. parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  59. var a = str.split(/[^0-9]/);
  60. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  61. }
  62. };
  63. module.exports = {
  64. formatTime: formatTime,
  65. formatLocation: formatLocation,
  66. dateUtils: dateUtils
  67. }