javascript - 如何将unix时间转换为yy-mm-dd

标签 javascript date unix-timestamp

我一直在寻找如何使用 javascript 将 UNIXTIME 转换为 YY-MM-DD 格式,但到目前为止我只找到了相反的方法(YY-MM-DD 到 UNIXTIME)。

jquery 中是否已经包含任何方法或其他方法来完成这项工作?

谢谢!!

最佳答案

您可以使用像 moments.js 这样的库或者在 POJS 中

Moments.js

A 5.5kb javascript date library for parsing, validating, manipulating, and formatting dates.

unixtime

Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970,[note 1] not counting leap seconds.[note 2] It is used widely in Unix-like and many other operating systems and file formats. Due to its handling of leap seconds, it is neither a linear representation of time nor a true representation of UTC.[note 3] Unix time may be checked on some Unix systems by typing date +%s on the command line.

Javascript Date object

Summary

Creates JavaScript Date instances which let you work with dates and times.

Javascript

function padZero(number) {
    if (number < 10) {
        number = "0" + number;
    }

    return number;
}

function unixtime2YYMMDD(unixtime) {
    var milliseconds = unixtime * 1000,
        dateObject = new Date(milliseconds),
        temp = [];

    temp.push(dateObject.getUTCFullYear().toString().slice(2));
    temp.push(padZero(dateObject.getUTCMonth() + 1));
    temp.push(padZero(dateObject.getUTCDate()));

    return temp.join("-");
}

console.log(unixtime2YYMMDD(1372069271));

输出

13-06-24 

关于jsfiddle

关于javascript - 如何将unix时间转换为yy-mm-dd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17354574/

相关文章:

Javascript 适用于除 Chrome 之外的所有浏览器

javascript - 如何从另一个函数扩展被调用的回调

rust - Solana Rust 智能合约如何获得区 block 高度或 Unix 时间?

api - Unix 时间戳或带时区的字符串?

javascript - 来自 promise 的 Angular $http.post 值

javascript - 使用 Vuetify 将修饰符添加到菜单激活器中的 v-on

MySQL 日期格式将小时-分钟转换为间隔并忽略年-月-日

mysql - 将 MySQL 日期时间读取为 Unix 时间

python 获取文件的时间戳,格式为 mm/dd/yyyy-HH :MM format

mysql - Mongodb : Query based on time in ISODate format. 我的查询有什么问题?