JavaScript透過Date-Mask將日期轉換成字串的方法_javascript技巧
本文实例讲述了JavaScript通过Date-Mask将日期转换成字符串的方法。分享给大家供大家参考。具体实现方法如下:
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程序设计有所帮助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

1.先開啟手機網路瀏覽器,搜尋微博網頁版,進入後點選左上角頭像按鈕。 2、然後點選右上角設定。 3.點選設定裡面的版本切換選項。 4.接著在版本切換裡選擇彩版選項。 5.點選搜索,進入搜尋頁面。 6.輸入關鍵字後,點選找人。 7.出來的搜尋完成介面點選篩選。 8.最後在發佈時間欄輸入特定日期後,點選篩選即可。

1.先說說我剛開始用的方法,或許大家也在用。先是打開【視野】——】備註模版【。 2.打開後確實能看到日期的地方。 3.先選擇它,並把它刪除。 4.刪除後點選【關閉母版檢視】。 5.再開啟列印預覽,發現日期還是在。 6.其實這個日期不是在這個地方刪除的。應該是在【講義母版】那裡。看下圖。 7.找到日期後把它刪除。 8.現在在打開預覽看一下,就沒有這個日期了。註:其實這個方法也很容易記,因為印刷的是講義,所以應該找【講義母版】。

excel軟體有非常強大的數據處理功能,我們經常用excel軟體來處理各種數據,有時我們在excel單元格中輸入日期的時候,excel打日期變成井號了,那怎麼能正常顯示數據呢?下面讓我們一起來看看解決的方法吧。 1.首先我們把滑鼠放在AB列之間的列寬線,雙擊以後調整列寬,如下圖所示。 2.列拉寬後,我們發現單元格內顯示的是數字,而不是日期,這肯定是不正確的,那我們就應該檢查一下單元格的格式了,如下圖所示。 3.點選“開始”標籤內的“數值”選項,點選下拉選單內的“其他數字格式”,如下圖所示。

PHP中int型別轉字串的方法詳解在PHP開發中,常會遇到將int型別轉換為字串型別的需求。這種轉換可以透過多種方式實現,本文將詳細介紹幾種常用的方法,並附帶具體的程式碼範例來幫助讀者更好地理解。一、使用PHP內建函數strval()PHP提供了一個內建函數strval(),可以將不同類型的變數轉換為字串類型。當我們需要將int型別轉換為字串型別時,

1.先開啟pycharm,進入到pycharm首頁。 2.然後新建python腳本,右鍵--點選new--點選pythonfile。 3.輸入一段字串,代碼:s="-"。 4.接著需要把字串裡面的符號重複20次,代碼:s1=s*20。5、輸入列印輸出代碼,代碼:print(s1)。 6.最後運行腳本,在最底部會看到我們的回傳值:-就重複了20次。

標題:Golang中判斷字串是否以指定字元結尾的方法在Go語言中,有時候我們需要判斷一個字串是否以特定的字元結尾,這在處理字串時十分常見。本文將介紹如何使用Go語言來實現這項功能,同時提供程式碼範例供大家參考。首先,讓我們來看看Golang中如何判斷一個字串是否以指定字元結尾的方法。 Golang中的字串可以透過索引來取得其中的字符,而字串的長度可

Go語言是一種強大且靈活的程式語言,它提供了豐富的字串處理功能,包括字串截取。在Go語言中,我們可以使用切片(slice)來截取字串。接下來,將詳細介紹如何在Go語言中截取字串,並附上具體的程式碼範例。一、使用切片截取字串在Go語言中,可以使用切片表達式來截取字串的一部分。切片表達式的語法如下:slice:=str[start:end]其中,s

Golang中如何檢查字串是否以特定字元開頭?在使用Golang程式設計時,經常會遇到需要檢查一個字串是否以特定字元開頭的情況。針對這項需求,我們可以使用Golang中的strings套件所提供的函數來實現。接下來將詳細介紹如何使用Golang檢查字串是否以特定字元開頭,並附上具體的程式碼範例。在Golang中,我們可以使用strings套件中的HasPrefix
