JS中获取当前时间点前一天时间

var date=new Date();
var dat_year=date.getYear();
var dat_month=date.getMonth();
var dat_day=date.getDay();
var dat_d=date.getDate();
// alert(dat_d);
var uom=new Date(dat_year,dat_month,dat_d);
// alert(uom);
// alert(date);
uom.setDate(uom.getDate()-1);
// alert(uom);
var new_dd=uom.getDate();
var new_mm=uom.getMonth();
var new_yy=uom.getFullYear();

JS实现将字符串转为Date类型的数据

var str="2012-09-20";
var dat = new Date(Date.parse(str.replace(/-/g,"/")));
var month=dat.getMonth();
alert(month);

eg:

var s = "2012/08/06";
var dateString = new Date(Date.parse(s));
alert(dateString.getMonth());
// var s = "2012/08/06 12:02:03";
var dateString = new Date(Date.parse(s));
alert(dateString.getMonth());
var t=dateString.toLocaleString();
var st=dateString.toLocaleTimeString();
alert(st);

Date类型常用方法

date.getYear();        //获取当前年份(2位)
date.getFullYear(); //获取完整的年份(4位,1970-????)
date.getMonth(); //获取当前月份(0-11,0代表1月)
date.getDate(); //获取当前日(1-31)
date.getDay(); //获取当前星期X(0-6,0代表星期天)
date.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
date.getHours(); //获取当前小时数(0-23)
date.getMinutes(); //获取当前分钟数(0-59)
date.getSeconds(); //获取当前秒数(0-59)
date.getMilliseconds(); //获取当前毫秒数(0-999)
date.toLocaleDateString(); //获取当前日期
var mytime=date.toLocaleTimeString(); //获取当前时间
date.toLocaleString( ); //获取日期与时间 即年月日 时分秒

还有一种高级的:

var today = new Date();
alert([
' 今天的日期为:' + today.toLocaleString(),
'七天前的日期为:' + new Date((+today) - 7*24*60*60*1000).toLocaleString()
].join('\n'));

还有按格式:

var s="2012/1/1 12:28:15";
var date=new Date(Date.parse(s));
alert(new Date((+date)-24*3600*1000).toLocaleString());

js中的各种获取日期的更多相关文章

  1. js中计算两个日期之差

    js中计算两个日期之差            var aBgnDate, aEndDate;            var oBgnDate, oEndDate;            var nYl ...

  2. Jquery UI 中的datepicker() ,获取日期后的回调函数onClose()

    <head> //引入相关的css/js <link rel="stylesheet" href="//code.jquery.com/ui/1.10. ...

  3. js中常用的获得日期对象的方法

    // 默认是当前时区的日期和时间 var date = new Date(); // 获取特定日期和时间的日期对象,需要传递毫秒数,不过可以传递规范日期格式字符串来代替 // 一些常见的日期格式 // ...

  4. js中如何去获取外部css样式

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  5. js中把ajax获取的数据转化成树状结构(并做成多级联动效果)

    1.首先通过ajax获取数据,此处省略,直接贴出获取到的数据格式 var arr = [{ id: 1, name: "一级标题", pid: 0 }, { id: 2, name ...

  6. 在html页面添加一个隐藏域,并渲染一个需要保存的数值,在js中需要再获取,而不影响页面结构

    <div style="display:none">可以将需要保存的数值放在文本内容中,也可以放在标签的属性当中, 如果放在文本内容中,注意换行后 \n 的存在< ...

  7. JS中Unix时间戳转换日期格式

    <!doctype html> <html> <head> <title>Unix时间戳转换成日期格式</title> <script ...

  8. js中如何快速获取数组中的最大值最小值

    var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math.min.apply(null, a));//最小值 多维数组可以这么修 ...

  9. js中style,currentStyle和getComputedStyle的区别以及获取css操作方法

    在js中,之前我们获取属性大多用的都是ele.style.border这种形式的方法,但是这种方法是有局限性的,该方法只能获取到行内样式,获取不了外部的样式.所以呢下面我就教大家获取外部样式的方法,因 ...

随机推荐

  1. android studio ,Gradle DSL method not found: 'compile()'

    用gradle构建android工程出现  Gradle DSL method not found: 'compile()' 错误 检查你外层的build.gradle文件中是不是用了compile方 ...

  2. C# 微信海报

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

  3. Save ITCM

    Debug String if below 64 BYTE it will in DRAM or it will in ITCM So to save ITCM , move it to ALLStr ...

  4. Java 多线程 并发编程

    一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...

  5. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

  6. 搭建企业cacti服务器

    搭建企业cacti服务器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 今天搭建了cacti,为了方便监控我的交换机~因为查了很多zabbix的资料关于监控交换机的教程~我都屡屡失 ...

  7. 最近兰州的js风格写个插件和一个template engine

    /* *@Product Name: Rational Framework Author: Calos Description: pager !important: pager */ (functio ...

  8. C++之路进阶——优先队列优化最短路径算法(dijkstra)

    一般的dijkstra算法利用贪心的思想,每次找出最短边,然后优化到其他点的的距离,我们还采用贪心思路,但在寻找最短边进行优化,之前是双重for循环,现在我们用优先队列来实现. 代码解释: //样例程 ...

  9. php扩展的基本安装

    phpize ./config --with-php-config=.. make&make install php.ini

  10. Oracle游标总结

    1.声明游标 declare teacher_id ); teacher_name ); teacher_title ); teacher_sex ); cursor teacher_cur is ; ...