js本地时间格式化

var myDate = new Date(); //获取当前时间及日期
var year=myDate.getYear(); // 获取当前年份(当前年份-1900)
var fyear=myDate.getFullYear(); // 获取完整的年份(4位,1970-????)
var mon=myDate.getMonth(); // 获取当前月份(0-11,0代表1月)
var date=myDate.getDate(); // 获取当前日(1-31)
var day=myDate.getDay(); // 获取当前星期X(0-6,0代表星期天)
var time=myDate.getTime(); // 获取当前时间(从1970.1.1开始的毫秒数)
var hos=myDate.getHours(); // 获取当前小时数(0-23)
var min=myDate.getMinutes(); // 获取当前分钟数(0-59)
var sec=myDate.getSeconds(); // 获取当前秒数(0-59)
var secs=myDate.getMilliseconds(); // 获取当前毫秒数(0-999)
var dd=myDate.toLocaleDateString(); // 获取当前日期
var mytime=myDate.toLocaleTimeString(); // 获取当前时间
var str=myDate.toLocaleString( ); // 获取日期与时间

Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小时
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var 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;
}

var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");

原文地址:https://www.cnblogs.com/wangxiaomo/p/12090444.html

时间: 2024-07-31 16:43:53

js本地时间格式化的相关文章

js -- 日期时间格式化

/** * js日期时间格式化 * @param date 时间读对象 * @param format 格式化字符串 例如:yyyy年MM月dd日 hh时mm分ss秒 * @returns {string} 返回格式化后的字符串 */function dateFormat (date, format) { var o = { "M+": date.getMonth() + 1, //month "d+": date.getDate(), //day "h+

js Date 时间格式化的扩展

js Date 时间格式化的扩展: 1 Date.prototype.format = function (fmt) { 2 var o = { 3 "M+": this.getMonth() + 1, //月 4 "d+": this.getDate(), //日 5 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //时 6 "H+": this.g

js日期/时间格式化方法

一.javascript Date format(日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:0

JS 自定义时间格式化

// 对Date的扩展,将 Date 转化为指定格式的String// 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("

js日期时间格式化

const formatTimes = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(do

PHP date 格式化一个本地时间/日期

PHP date 格式化一个本地时间/日期 date (PHP 4, PHP 5) date — 格式化一个本地时间/日期 说明 string date ( string $format [, int $timestamp ] ) 返回将整数 timestamp 按照给定的格式字串而产生的字符串.如果没有给出时间戳则使用本地当前时间.换句话说,timestamp 是可选的,默认值为 time(). Tip 自 PHP 5.1.1 起有几个有用的常量可用作标准的日期/时间格式来指定 format 

js时间格式化函数,支持Unix时间戳

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"> <title>js时间格式化函数,支持Unix时间戳</title> </head>

js对特殊字符转义、时间格式化、获取URL参数

/*特殊字符转义*/ function replace_html(str) { var str = str.toString().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"'); return str; } /* *时间格式化 *例子:time = new Date().Format(

js 时间格式化

// 时间格式化Date.prototype.format = function(format) { /* * eg:format="yyyy-MM-dd hh:mm:ss"; */ var o = { "M+" : this.getMonth() + 1, // month "d+" : this.getDate(), // day "h+" : this.getHours(), // hour "m+"