【问题标题】:Convert Json timestamp to normal date and time in javascript在javascript中将Json时间戳转换为正常的日期和时间
【发布时间】:2012-02-15 08:42:12
【问题描述】:

我有一个 Json 时间戳,我想使用 javascript 将其转换为简单的日期时间格式。

我需要以下格式的日期和时间:dd-mm-yyyy hr:mn

这是我希望提取时间戳的示例 json 日期:“timestamp”:1326439500

{
   "count": 2,
   "d": [
      {
         "title": "Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)",
         "description": "Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone",
         "link": "http://wik.io/info/US/309201303",
         "timestamp": 1326439500,
         "image": null,
         "embed": null,
         "language": null,
         "user": null,
         "user_image": null,
         "user_link": null,
         "user_id": null,
         "geo": null,
         "source": "wikio",
         "favicon": "http://wikio.com/favicon.ico",
         "type": "blogs",
         "domain": "wik.io",
         "id": "2388575404943858468"
      },
      {
         "title": "Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)",
         "description": "SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone",
         "link": "http://wik.io/info/US/309198933",
         "timestamp": 1326439320,
         "image": null,
         "embed": null,
         "language": null,
         "user": null,
         "user_image": null,
         "user_link": null,
         "user_id": null,
         "geo": null,
         "source": "wikio",
         "favicon": "http://wikio.com/favicon.ico",
         "type": "blogs",
         "domain": "wik.io",
         "id": "16209851193593872066"
      }
   ]
} 

【问题讨论】:

  • var date=new Date(timestamp)var hours = date.getHours(); // 时间戳的分钟部分 var minutes = date.getMinutes(); // 时间戳的秒部分 var seconds = date.getSeconds(); // 将以 10:30:23 格式显示时间 var formattedTime = hours + ':' + minutes + ':' + seconds;
  • @RajatSinghal 只会给出时间,不是吗?
  • 您可以从 Date 对象中获取任何日期、年份...而不是 gethours 使用 getYear...
  • json 没有时间戳。它没有日期/时间的概念——它只是一个传输封装。该数字是生成 json 文本的任何语言的时间戳。
  • @RajatSinghal 谢谢它的帮助。

标签: javascript json datetime timestamp simpledateformat


【解决方案1】:

扩展Date 的原型以包含这样的格式函数(或查找或创建您自己的):

Date.prototype.format = function (formatString) {
    // Returns a formatted date string
    var month = this.getMonth() + 1,
        day = this.getDate(),
        year = this.getFullYear(),
        hours24 = this.getHours(),
        hours = (hours24 === 0 ? 12 : hours24 > 12 ? hours24 - 12 : hours24),
        meridiem = hours24 >= 12 ? "PM" : "AM",
        minutes = this.getMinutes(),
        seconds = this.getSeconds();

    return formatString.replace(/(MM)/g, month.padLeft(2, '0'))
        .replace(/(M)/g, month)
        .replace(/(dd)/g, day.padLeft(2, '0'))
        .replace(/(d)/g, day)
        .replace(/(yyyy)/ig, year)
        .replace(/(yy)/ig, year.toString().substring(2, 4))
        .replace(/(hh)/g, hours.padLeft(2, '0'))
        .replace(/(h)/g, hours)
        .replace(/(HH)/g, hours24.padLeft(2, '0'))
        .replace(/(H)/g, hours24)
        .replace(/(mm)/g, minutes.padLeft(2, '0'))
        .replace(/(m)/g, minutes)
        .replace(/(ss)/g, seconds.padLeft(2, '0'))
        .replace(/(s)/g, seconds)
        .replace(/(tt)/g, meridiem.toLowerCase())
        .replace(/(TT)/g, meridiem);
};

然后,要将时间戳转换为所需的格式,dd-mm-yyyy hr:mn(如您的评论中所述),您需要执行以下操作:

var dateString = new Date(timestamp).format("dd-MM-yyyy hh:mm");

[编辑]下面是附带的pad函数:

Number.prototype.padLeft = function (width, padChar) {
    // Returns a padded string
    padChar = padChar || ' ';
    var value = this.toString();
    while (value.length < width) {
        value = padChar + value;
    }
    return value;
};

【讨论】:

  • 哇,非常全面。感谢您花时间解释这一点。我现在不能投票,因为有人标记了我的问题,否则我会对你的答案投票。再次感谢
【解决方案2】:

日期以自纪元以​​来的毫秒数返回。下面的代码创建了一个 JS 日期对象:

var d = new Date(1245398693390);
var formattedDate = d.getDate() + "-" + (d.getMonth() + 1) + "-" + d.getFullYear();
var hours = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
var formattedTime = hours + ":" + minutes;

formattedDate = formattedDate + " " + formattedTime;

这里是a working fiddle

【讨论】:

  • 感谢伙伴提供一个简单的例子。但这给了我这个整体:2009-06-19T08:04:53.390Z 我想要类似 dd-mm-yyyy hr:mn
  • @praneybehl,我误解了。你只是想要时间?
  • 不,我需要这种格式的日期时间:dd-mm-yyyy hr:mn
  • @praneybehl,我认为您应该将该信息添加到您的原始问题中 - 它没有说明日期格式。
  • 如果我问了这样一个菜鸟问题而冒犯了某人,我很抱歉,因为我看到有人因为不恰当而提出了“标记下来”的问题。我自己刚开始学习 Javascript,这个论坛是一个真正的救星。
【解决方案3】:
<script>
var timestamp=1326439320;
var date=new Date(timestamp);
var hours = date.getHours(); // minutes part from the timestamp
var minutes = date.getMinutes(); // seconds part from the timestamp
var seconds = date.getSeconds(); // will display time in 10:30:23 format
 var formattedTime = hours + ':' + minutes + ':' + seconds;
alert(formattedTime);
</script>

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2014-08-01
    • 2023-03-26
    相关资源
    最近更新 更多