Skip to content

Instantly share code, notes, and snippets.

@MOOOWOOO
Last active December 2, 2016 01:59
Show Gist options
  • Select an option

  • Save MOOOWOOO/703b817b4b53841c82564be3d1e06825 to your computer and use it in GitHub Desktop.

Select an option

Save MOOOWOOO/703b817b4b53841c82564be3d1e06825 to your computer and use it in GitHub Desktop.
date function list of js.
function calcTime(city, offset) {
let d = new Date();
let utc = d.getTime() + (d.getTimezoneOffset() * 60000);
let nd = new Date(utc + (3600000 * offset));
let gmtTime = new Date(utc);
let day = nd.getDate();
let month = nd.getMonth();
let year = nd.getYear();
let hr = nd.getHours(); //+ offset
let min = nd.getMinutes();
let sec = nd.getSeconds();
if (year < 1000) {
year += 1900;
}
let monthArray = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
let monthDays = [
"31",
"28",
"31",
"30",
"31",
"30",
"31",
"31",
"30",
"31",
"30",
"31"
];
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
monthDays[1] = "29";
}
if (hr >= 24) {
hr = hr - 24;
day -= -1;
}
if (hr < 0) {
hr -= -24;
day -= 1;
}
if (hr < 10) {
hr = " " + hr;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
if (day <= 0) {
if (month == 0) {
month = 11;
year -= 1;
}
else {
month = month - 1;
}
day = monthDays[month];
}
if (day > monthDays[month]) {
day = 1;
if (month == 11) {
month = 0;
year -= -1;
}
else {
month -= -1;
}
}
return city + ":The local time is" + monthArray[month] + " " + day + ", " + year + "<br>" + hr + ":" + min + ":" + sec;
//return "The local time in " + city + " is " + nd.toLocaleString()+;
}
let d = new Date(),
c = Date.now();
console.log(d);
// 2016/6/19 上午11:30:40
console.log(d.toLocaleString());
// 2016/6/19
console.log(d.toLocaleDateString());
// Sun Jun 19 2016
console.log(d.toDateString());
// 2016-06-19T03:30:40.096Z
console.log(d.toISOString());
// 上午11:30:40
console.log(d.toLocaleTimeString());
// 11:30:40 GMT+0800 (CST)
console.log(d.toTimeString());
// 2016-06-19T03:30:40.096Z
console.log(d.toJSON());
// Sun, 19 Jun 2016 03:30:40 GMT
console.log(d.toUTCString());
// Sun Jun 19 2016 11:30:40 GMT+0800 (CST)
console.log(d.toString());
// 1466307040096
console.log(c);
// 1,466,307,040,096
console.log(c.toLocaleString());
Date.prototype.pattern = function (fmt) {
let o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S" : this.getMilliseconds() //毫秒
};
let week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
// let t = new Date(1465068304000);
let t = new Date();
console.log(t.pattern("MMddHHmmyyyy.ss"));
console.log(t.pattern("yyyy-MM-dd HH:mm:ss"));
// format seconds to hh:mm:ss
let formatSeconds = function (value) {
let sec = parseInt(value);// 秒
let min = 0;// 分
let hour = 0;// 小时
if (sec > 60) {
min = parseInt(sec / 60);
sec = parseInt(sec % 60);
if (min > 60) {
hour = parseInt(min / 60);
min = parseInt(min % 60);
}
}
return hour + ':' + min + ':' + sec;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment