本文实例讲述了js模仿php中strtotime()与date()函数实现方法。分享给大家供大家参考。具体如下:

在js中没有像php中strtotime()与date()函数,可直接转换时间戳,下面我们来自定一个函数来实现js中具体有时间戳转换的功能。

function datetime_to_unix(datetime){

var tmp_datetime = datetime.replace(/:/g,'-');

tmp_datetime = tmp_datetime.replace(/ /g,'-');

var arr = tmp_datetime.split("-");

var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));

return parseInt(now.getTime()/1000);

}

function unix_to_datetime(unix) {

var now = new Date(parseInt(unix) * 1000);

return now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

var datetime = '2012-11-16 10:36:50';

var unix = datetime_to_unix(datetime);

document.write(datetime+' 转换后的时间戳为: '+unix+'

');

var unix = 1353033300;

var datetime = unix_to_datetime(unix);

document.write(unix+' 转换后的日期为: '+datetime);

如果想弹出:2010-10-20 10:00:00这个格式的也好办

function getLocalTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

alert(getLocalTime(1177824835));

完整实例

var day1 = parseInt(new Date().valueOf()/1000);

var day2 = new Date(day1 * 1000);

function getLocalTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');

}

/* 同上面函数 */

function getLocalTimes(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);

}

function getLocalFormatTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

document.getElementById("btn1").onclick = function(){

alert(day1);

}

document.getElementById("btn2").onclick = function(){

alert(day2.toLocaleString());

}

document.getElementById("btn3").onclick = function(){

alert( getLocalTime(day1) );

}

document.getElementById("btn4").onclick = function(){

alert( getLocalFormatTime(day1) );

}

document.getElementById("btn5").onclick = function(){

alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds());

}

希望本文所述对大家的javascript程序设计有所帮助。

java strtotime_js模仿php中strtotime()与date()函数实现方法相关推荐

  1. php中strtotime(date,js模仿php中strtotime()与date()函数实现方法

    本文实例讲述了js模仿php中strtotime()与date()函数实现方法.分享给大家供大家参考.具体如下: 在js中没有像php中strtotime()与date()函数,可直接转换时间戳,下面 ...

  2. java 控制器 返回 json_SpringMVC中controller返回json数据的方法

    java 控制器 返回 json_SpringMVC中controller返回json数据的方法 1.方法一: 使用springmvc原生注解@ResponseBody @ResponseBody @ ...

  3. Py之pandas:pandas的read_excel()函数中各参数说明及函数使用方法讲解

    Py之pandas:pandas的read_excel()函数中各参数说明及函数使用方法讲解 目录 pandas的read_excel()函数中各参数说明及函数使用方法讲解 read_excel()函 ...

  4. [转]在C#中使用API回调函数的方法

    在C#中使用API回调函数的方法 就以EnumChildWindows和EnumChildProc为例子: 首先要声明EnumChildProc 为一个回调函数 public delegate boo ...

  5. Js文件中调用其它Js函数的方法(转)

    2019独角兽企业重金招聘Python工程师标准>>> Js文件中调用其它Js函数的方法 在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分 ...

  6. Js文件中调用其它Js函数的方法

    2019独角兽企业重金招聘Python工程师标准>>> Js文件中调用其它Js函数的方法 在项目开发过程中,也许你会遇这样的情况.在某一Js文件中需要完成某一功能,但这一功能的大部分 ...

  7. vue怎么调用子元素的方法_vue 父组件中调用子组件函数的方法

    vue 父组件中调用子组件函数的方法 在父组件中调用子组件的方法: 1.给子组件定义一个ref属性.eg:ref="childItem" 2.在子组件的methods中声明一个函数 ...

  8. disp语句怎么格式 matlab_Matlab中disp和sprintf函数使用方法和区别介绍

    Matlab中disp和sprintf函数使用方法和区别介绍 作者:小安 来源:PC下载网时间:2020-08-17 17:48:55 相信各位都知道的,Matlab自带很多函数,用处各不相同,当中, ...

  9. java内联_JAVA中的内联函数

    在说内联函数之前,先说说函数的调用过程. 调用某个函数实际上将程序执行顺序转移到该函数所存放在内存中某个地址,将函数的程序内容执行完后,再返回到 转去执行该函数前的地方.这种转移操作要求在转去前要保护 ...

最新文章

  1. matlab估计arma残差,写给你的金融时间序列分析:补完篇
  2. 又到了年终总结的时候了,数据分析的年度工作计划,你知道该怎么写吗?
  3. SAP ABAP实用技巧介绍系列之 template的match顺序
  4. windows安全模式_Windows 安全模式的功能和作用
  5. iPhone 的续航有望加长!749 元的“磁吸充电宝”你会买吗?
  6. [object%20HTMLInputElement] 是什么意思?
  7. leetcode 172. Factorial Trailing Zeroes(阶乘的末尾有多少个0)
  8. 从Visual studio 2005移出Visual Assist
  9. *第六周*数据结构实践项目二【建设链栈算法库】
  10. linux下oracle完全卸载,linux环境下卸载oracle 11g
  11. arccatalog点要素显示不完_利用ArcCatalog 管理地理空间数据库
  12. 无聊之时用css3自制了好看的button样式和input样式
  13. week15-作业题--字符串(hash、字典树、KMP)
  14. 漫画:从今天开始,我不再买书了!
  15. python基础教程第三版pdf 脚本之家-一篇不错的Python入门教程
  16. Android开发——数据库框架Suger遇到的大坑(Gson和Suger的复用Bean请见“大坑三”)
  17. Vue 获取当前日期
  18. LabVIEW MasterFramework 源码下载入口
  19. 计算机算法设计与分析(第五版)---王晓冬--------前六章(递归与分治,动态规划,贪心算法,回溯法,)期末复习资料总结
  20. 【牛客 错题集】Linux系统方面错题合集

热门文章

  1. linux下各类常用make命令解释(make/make clean/make distclean)
  2. 第七篇:面向对象高级
  3. 【Google Code Jam】Millionaire
  4. ios7查询摄像头使用权限
  5. 模拟芯片测试之CONT测试
  6. win10 优化批处理
  7. 关于东软医保和首信医保及其他医保接口的开发
  8. 第三届广西大学东信杯题解(感谢中国东信爸爸赞助支持)
  9. 微软智能云三驾马车Azure、Office 365、Dynamics 365齐聚中国
  10. 设计模式之——封装、继承、多态