Home > Web Front-end > JS Tutorial > js gets time and converts between string and timestamp_javascript skills

js gets time and converts between string and timestamp_javascript skills

WBOY
Release: 2016-05-16 16:22:10
Original
1322 people have browsed it

Stop talking nonsense and get straight to the code

Copy code The code is as follows:

//Get the current time:
var myDate = new Date();//Current time
var year = myDate.getFullYear();//Current year
var month = myDate.getMonth() 1;//Current month
var day = myDate.getDate();//Current day
myDate.getYear(); //Get the current year (2 digits)
MyDate.getFullYear(); //Get the complete year (4 digits, 1970-????)
myDate.getMonth(); //Get the current month (0-11, 0 represents January)
myDate.getDate(); //Get the current day (1-31)
myDate.getDay(); //Get the current week X (0-6, 0 represents Sunday)
MyDate.getTime(); //Get the current time (the number of milliseconds since 1970.1.1)
myDate.getHours(); //Get the current hours (0-23)
MyDate.getMinutes(); //Get the current minutes (0-59)
MyDate.getSeconds(); //Get the current seconds (0-59)
MyDate.getMilliseconds(); //Get the current number of milliseconds (0-999)
MyDate.toLocaleDateString(); //Get the current date
var mytime=myDate.toLocaleTimeString(); //Get the current time
myDate.toLocaleString(); //Get date and time
var oneDay = 1000 * 60 * 60 * 24;
//Get the date of the latest week
var lastDate = new Date(myDate - oneDay * 6);
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() 1;
var lastDay = lastDate.getDate();
//Get the last day of the current month
var day = new Date(year ,month , 0);
var lastdate = day.getDate();//The last day of the current month
//Get the date of the last N months
var lastDate = new Date(myDate - oneDay * myDate.getDate());
lastDate = new Date(lastDate - N * oneDay * (lastDate.getDate() - 1));
var lastYear = lastDate.getFullYear();
var lastMonth = lastDate.getMonth() 1;
var lastDay = lastDate.getDate();
//Convert string to timestamp
var date="2014-12-06";
date = new Date(Date.parse(date.replace(/-/g, "/")));
date = date.getTime();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template