JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法
更新時間:2015年06月04日 17:08:51 作者:不吃皮蛋
這篇文章主要介紹了JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法,涉及javascript日期、數(shù)組及字符串操作的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了JavaScript通過Date-Mask將日期轉(zhuǎn)換成字符串的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
var MonthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var DayNames = [ "Sunday", "Monday", "Tueday", "Wednesday", "Thursday",
"Friday", "Saturday" ];
var ShortMths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
var ShortDays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var StringToDate = function (sDate, sFormat, cutOff) {
// Input: a date value as a string, it's format as a string e.g. 'dd-mmm-yy'
// Optional: a cutoff (integer) for 2 digit years.
// If no 'd' appears in the format string then the 1st of the month is assumed.
// If the year is 20 and the cut-off is 30 then the value will be converted
// to 2020; if the year is 40 then this will be converted to 1940.
// If no cut-off is supplied then '20' will be pre-pended to the year (YY).
// Output: a string in the format 'YYYY/MM/DD' or ''
// Will not attempt to convert certain combinations e.g. DMM, MDD, DDM, YYYYD.
var sParsed, fndSingle;
// sParsed will be constructed in the format 'YYYY/MM/DD'
sDate = sDate.toString().toUpperCase();
sFormat = sFormat.toUpperCase();
if (sFormat.search(/MMMM|MMM/) + 1) { // replace Mar/March with 03, etc.
sDate = sDate.replace(new RegExp('(' + ShortMths.join('|') + ')[A-Z]*', 'gi'),
function (m) {
var i = ShortMths.indexOf(m.charAt(0).toUpperCase() +
m.substr(1, 2).toLowerCase()) + 1;
return ((i < 10) ? "0" + i : "" + i).toString();
});
sFormat = sFormat.replace(/MMMM|MMM/g, 'MM');
}
if (sFormat.search(/DDDD|DDD/) + 1) { // replace Tue/Tuesday, etc. with ''
sDate = sDate.replace(new RegExp('(' + ShortDays.join('|') + ')[A-Z]*', 'gi'),'');
sFormat = sFormat.replace(/DDDD|DDD/g, '');
}
sDate = sDate.replace(/(^|\D)(\d)(?=\D|$)/g, function($0, $1, $2) {
// single digits 2 with 02
return $1 + '0' + $2;
});
sFormat = sFormat.replace(/(^|[^DMY])(D|M)(?=[^DMY]|$)/g, function($0, $1, $2){
return $1 + $2 + $2; // replace D or M with DD and MM
});
// are there still single Ds or Ms?
fndSingle = sFormat.search(/(^|[^D])D([^D]|$)|(^|[^M])M([^M]|$)/)+1;
// do not attempt to parse, for example, 'DMM'
if ( fndSingle ) return '';
sFormat = sFormat.replace(/(^|[^Y])(YY)(?=[^Y]|$)/g, function($0, $1, $2, index) {
var tempDate = sDate.substr(0, index + 1);
tempDate += (cutOff) ? ((parseInt(sDate.substr(index + 1, 2),10) > cutOff) ? '19' : '20') : '20';
tempDate += sDate.substr(index + 1);
sDate = tempDate;
return $1 + $2 + $2;
});
sParsed = ('YYYY/MM/DD').replace(/YYYY|MM|DD/g, function(m){
return (sFormat.indexOf(m) + 1) ?
sDate.substr(sFormat.indexOf(m), m.length) : '';
});
if (sParsed.charAt(0) == '/') {
// if no year specified, assume the current year
sParsed = (new Date().getFullYear()) + sParsed;
}
if (sParsed.charAt(sParsed.length - 1) == '/') {
// if no date, assume the 1st of the month
sParsed += '01';
}
// should end up with 10 characters..
return ( sParsed.length == 10 ) ? sParsed : '';
};
希望本文所述對大家的javascript程序設(shè)計有所幫助。
您可能感興趣的文章:
- js用Date對象的setDate()函數(shù)對日期進行加減操作
- js字符串日期yyyy-MM-dd轉(zhuǎn)化為date示例代碼
- JS中Date日期函數(shù)中的參數(shù)使用介紹
- js實現(xiàn)的日期操作類DateTime函數(shù)代碼
- Javascript日期對象的dateAdd與dateDiff方法
- 淺談JS日期(Date)處理函數(shù)
- JavaScript Date對象 日期獲取函數(shù)
- Javascript 日期對象Date擴展方法
- javascript中Date format(js日期格式化)方法小結(jié)
- js日期插件dateHelp獲取本月、三個月、今年的日期
- 淺談JavaScript Date日期和時間對象
- JavaScript日期對象(Date)基本用法示例
相關(guān)文章
JavaScript判斷一個變量是否是數(shù)組的五種方式總結(jié)
在 JavaScript 編程中,我們經(jīng)常需要對不同類型的變量進行判斷和處理,其中,判斷一個變量是否是數(shù)組是一項基本且常見的任務(wù),在本篇博客中,我們將介紹幾種常用的方式來判斷一個變量是否是數(shù)組,并探討它們的優(yōu)缺點以及適用場景,需要的朋友可以參考下2023-11-11
深入理解javascript動態(tài)插入技術(shù)
這篇文章介紹了javascript動態(tài)插入技術(shù),有需要的朋友可以參考一下2013-11-11
在原生不支持的舊環(huán)境中添加兼容的Object.keys實現(xiàn)方法
下面小編就為大家?guī)硪黄谠恢С值呐f環(huán)境中添加兼容的Object.keys實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
20分鐘成功編寫bootstrap響應(yīng)式頁面 就這么簡單
這篇文章主要教大家如何在20分鐘內(nèi)成功編寫bootstrap響應(yīng)式頁面,其實很簡單,培養(yǎng)大家分分鐘開發(fā)出一個高大上的頁面能力,感興趣的小伙伴們可以參考一下2016-05-05

