原文地址:http://www.sufeinet.com/thread-1500-1-1.html

js时间戳怎么转成日期格式
这个在主群里有朋友§☆釺哖蟲...o问js时间戳怎么转成日期格式 ,他的问题是这样的
/Date(1354116249000)/ 这样的格式怎么转成时间格式
这是从C#的Datatime格式通过Json传到Js里面的,
下面是我们的提供的方法
js需要把时间戳转为为普通格式,一般的情况下可能用不到的,
下面先来看第一种吧

<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}
alert(getLocalTime(1293072805));
</script>

结果是
2010年12月23日 10:53
第二种

<script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)}
alert(getLocalTime(1293072805));
</script>

如果你想得到这样格式的怎么办呢?
2010-10-20 10:00:00
看下面代码吧

 <script>
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
alert(getLocalTime(1177824835));
</script>

也可以这样写的

function   formatDate(now)   {
var year=now.getYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
} var d=new Date(1230999938);
alert(formatDate(d));

好了问题解决
需要注意的是
不要把字符串中的Date(这样的字符也传进去,要先处理一下,这样很方便 就能处理的
可以使用replace方法
如下:

replace("/Date(","").replace(")/","");

js时间戳怎么转成日期格式的更多相关文章

  1. js将时间戳装换成日期格式

    13位时间戳改为yyyy-MM-dd HH-mm-ss 格式 目标时间戳:1516324500000 formatDateTime (unix) { // 转换时间戳 var date = new D ...

  2. 用js 将long类型转换成日期格式

    //扩展Date的format方法 Date.prototype.format = function (format) { var o = { "M+": this.getMont ...

  3. js时间戳差值转日期格式

    <script> function getRemainderTime (startTime){     var s1 = new Date(startTime.replace(/-/g,  ...

  4. js 时间戳转换为指定的日期格式

      function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFul ...

  5. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd

    昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...

  6. js时间戳转成日期格式

    将时间戳转换成日期格式:// 简单的一句代码var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000.比如php函数time()获得的时间戳 ...

  7. web 前端 常见操作 将时间戳转成日期格式 字符串截取 使用mui制作选项卡

    1.将时间戳转成日期格式: //第一种 function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString( ...

  8. 将序列化成json格式的日期(毫秒数)转成日期格式

    <script> $(function () { loadInfo(); }) function loadInfo() { $.post("InfoList.ashx" ...

  9. CTime,Systemtime的比较还有转换成日期格式。

    vc为我们提供了两种日期型的变量. 一种是CTime.他的缺点就是年份只支持到2038年,以后的日期就不支持啦,如果你的项目有20-30年的寿命,你就选择使用SYSTEMTIME.这个时间函数来进行比 ...

随机推荐

  1. 堆排序之Java实现

    堆排序之Java实现 代码: package cn.com.zfc.lesson21.sort; /** * * @title HeapSort * @describe 堆排序 * @author 张 ...

  2. Codeforces.666A.Reberland Linguistics(DP)

    题目链接 \(Description\) 给定串s,其由一个基本串后加任意多个长度为2或3的后缀串构成,要求基本串长度>4且相邻后缀串不相同.在基本串任意确定的情况下,求所有可能的后缀串. \( ...

  3. Codeforces Round #505 (Div 1 + Div 2) (A~D)

    目录 Codeforces 1025 A.Doggo Recoloring B.Weakened Common Divisor C.Plasticine zebra D.Recovering BST( ...

  4. nodejs备忘总结(一) -- 基础入门

    什么是NodeJS JS是脚本语言,脚本语言都需要一个解析器才能运行.对于写在HTML页面里的JS,浏览器充当了解析器的角色.而对于需要独立运行的JS,NodeJS就是一个解析器. 每一种解析器都是一 ...

  5. securecrt中文乱码以及ubuntu设置locale

    参考文献 http://wiki.ubuntu.org.cn/%E4%BF%AE%E6%94%B9locale http://www.bootf.com/547.html 强烈建议 ubuntu下面不 ...

  6. Cloud Foundry中DEA启动应用实例时环境变量的使用

    在Cloud Foundry v2中,当应用用户须要启动应用的实例时.用户通过cf CLI向cloud controller发送请求,而cloud controller通过NATS向DEA转发启动请求 ...

  7. Property's synthesized getter follows Cocoa naming convention for returning

    Property's synthesized getter follows Cocoa naming convention for returning.   今天早上在整理代码的时候发现了如上警告. ...

  8. iOS 创建单例的两种方法

    创建一个单例很多办法.我先列举一个苹果官方文档中的写法. [cpp] view plaincopy static AccountManager *DefaultManager = nil; + (Ac ...

  9. poj 3041(最大匹配问题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  10. ibatis.net:第六天,QueryForList

    xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...