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?

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line