用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
得到后台从数据库中拿到的数据我们希望格式是
2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30
然而我们前台得到的却是一段数字(时间戳,毫秒数)
1477386005
我们要将时间戳转化为我们想要的格式。
核心方法 :
1477386005是我从后台得到时间戳 (注意:有的时候得到的时间戳是已经乘以1000的)
var unixTimestamp = new Date( 1477386005*1000 ) ;
commonTime = unixTimestamp.toLocaleString();
alert(commonTime);
这时候的结果是:

但是我希望转换为我自己想要的格式,就在本页面重写一下 toLocaleString()方法即可。
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
};
结果为:

或者其他想要的格式:
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
};
结果为:

友情链接:http://unixtime.51240.com/ 时间戳转化网站
http://www.w3school.com.cn/jsref/jsref_toLocaleString.asp w3cschool
用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式的更多相关文章
- JS将后台获取毫秒数转换为自定义格式日期
重写prototype Date.prototype.Format = function(fmt) { var o = { "M+" : this.getMonth()+1, / ...
- js毫秒数转换为具体日期
[1].毫秒数转换为具体日期 function getMyDate(str) { var oDate = new Date(str), oYear = oDate.getFullYear( ...
- 毫秒数转换为指定格式日期的js代码
var format = function(time, format){ var t = new Date(time); var tf = function(i){return (i < 10 ...
- delphi 获得时间戳 毫秒数
function DateTimeToMilliseconds(const ADateTime: TDateTime): Int64; //获得毫秒var LTimeStamp: TTimeStamp ...
- js时间戳与日期格式的相互转换
下面总结一下js中时间戳与日期格式的相互转换: 1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(ti ...
- js 时间戳转为日期格式
原文:js 时间戳转为日期格式 js 时间戳转为日期格式 什么是Unix时间戳(Unix timestamp): Unix时间戳(Unix timestamp),或称Unix时间(Unix time) ...
- js中时间戳与日期格式的相互转换
1. 将时间戳转换成日期格式: function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//时间戳为10 ...
- 【JavaScript】标准日期、中国标准时间、时间戳、毫秒数互转
转载自:https://blog.csdn.net/IT429/article/details/78341847 看到的一篇比较有用的前端js时间转换方法,留个备份 首先要明确这三种格式是什么样子的: ...
- js日期格式,日期对象
以对象为基准去使用方法, 围绕Date对象 var a = new Date() 返回当前的时间对象,可以使用内置的日期对象的方法 a.getFullYear(), a.getMonth(), a.g ...
随机推荐
- ACM知识点
基础算法 高精 模拟 分治 贪心 排序 DFS 迭代加深搜索 BFS 双向BFS 动态规划 DAG上DP 树上DP 线性DP 图算法 最短路 FLYD DJATL BF 最大流 Dinic ISAP ...
- redhat7下配置tomcat7,更改端口8080后无法访问页面
搞了一下午,居然是防火墙的事情,redhat7设置方法如下 sudo firewall-cmd --add-port=8081/tcp
- leetcode-【hard】4. Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 【SSM 4】Mybatis逆向生成工具
在上一篇博客中说到,Mybatis是灵活的SQL语句应用,不想Hibernate一样有其封装好的方法,那么,当我们用Mybatis的时候(Hibernate),我们都需要编写其实体类,和配置文件.本篇 ...
- ATL中窗口句柄与窗口过程的关联方法
ATL中采用了一种动态生成机器指令的方式进行窗口句柄与窗口对象进行关联,以是详细分析: CWindowImpl会在第一次调用Create时注册窗口类,该窗口类是的信息是在CWindowImpl的子类中 ...
- 笔试常考的Java基础
1. Socket编程:ServerSocket (int port) :Creates a server socket, bound to the specified port. Socket(In ...
- 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1
本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...
- shell判断FTP传输是否成功
#!/bin/bash ##判断FTP传输文件是否成功 exec 6>&1 1>/tmp/lzc.txt ##打开一个文件描述符6,保存文件描述符1的属性,然后将描述1重定向到lz ...
- Nginx使用Expires增加浏览器缓存加速
Max-age是指我们的web中的文件被用户访问(请求)后的存活时间,是个相对的值,相对Request_time(请求时间). Expires它比max-age要麻烦点,Expires指定的时间分&q ...
- node代码片段
/** * Created by Administrator on 2016/8/22 0022. * chat */ var net=require('net'); var chatServer=n ...