doumei8258 2014-03-10 10:59
浏览 87
已采纳

将钛的时间段转换为日期

I have epoch times stored in my database like so: 1392821307. These were picked up on a separate web service using the php time() function. I need to convert this to a date/month/year format in Titanium.

So far I have used the following code based off an answer here in stack overflow

var utcSeconds = Jobs_data.Jobs[i].DatePosted;
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(utcSeconds);

This returns a string like so:

Wed Feb 19 14:48:27 GMT 2014

I only need the Feb 19 2014. So I'm wondering how to parse this string. I was going to use substring but then for example, Mon will have 3 letters and thurs will have 4, so the starting index of where to parse is changing. Not sure if the month will be of varying length also

  • 写回答

1条回答 默认 最新

  • doulubashang6936 2014-03-10 17:55
    关注

    d is actually a date object. If you pass that to Ti.API.info, or console.log, it'll be coerced in to the string that you see above. You should use its getDate, getMonth, and getFullYear methods to get the string that you want.

    var formattedString = (d.getMonth()+1) + '/' + d.getDate() + '/' + d.getFullYear();
    

    Alternatively, use moment.js, which is included in Alloy apps, and can be easily downloaded in to vanilla Titanium apps too:

    var moment = require('alloy/moment'),
        dm = moment(d),
        formattedString = dm().format('L');
    

    This is answered quite simply by a SO answer already:

    Where can I find documentation on formatting a date in JavaScript?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本