顯示具有 Javascript 標籤的文章。 顯示所有文章
顯示具有 Javascript 標籤的文章。 顯示所有文章

2022年6月21日 星期二

Javascript 上個月、本月、下個月

參考了網路寫了一個可直接設定第一天跟最後一天
//上個月
$(document).on('click', '#LastMonth', function () {                        
	SetLastAndFirstDate($('#fromdate').val(), -1);
})
//本月
$(document).on('click', '#ThisMonth', function () {            
	SetLastAndFirstDate($('#fromdate').val(), 0);
})
//下個月
$(document).on('click', '#NextMonth', function () {            
	SetLastAndFirstDate($('#fromdate').val(), +1);
})

function SetLastAndFirstDate(nowday, tomonth) {
	var date = new Date(nowday);

	if (tomonth == 0)
		var date = new Date();
	
	var firstDay = new Date(date.getFullYear(), date.getMonth() + (0 + tomonth), 1);
   var lastDay = new Date(date.getFullYear(), date.getMonth() + (1+tomonth), 0);
   $('#fromdate').val(formatDate(firstDay));//設定第一天
   $('#todate').val(formatDate(lastDay));//設定最後一天
   
}       

function formatDate(date) {
	var d = new Date(date),
	month = '' + (d.getMonth() + 1),
	day = '' + d.getDate(),
	year = d.getFullYear();
 	if (month.length < 2)
		month = '0' + month;
	if (day.length < 2)
	day = '0' + day;
	return [year, month, day].join('-');
}
參考來源
Format JavaScript date as yyyy-mm-dd
取得當月第一天和最後一天

2014年10月30日 星期四

Javascript 三位一撇

懶的再改程式了,所以想直接在前端攪定
找了一下很快就找到了
 
       function moneyFormat(str) {
            if (str.length <= 3) {
                return str;
            }
            else {
                return moneyFormat(str.substr(0, str.length - 3)) + "," + (str.substr(str.length - 3));
            }
        } 
2020-12-23補
 
 
    //三位一撇
    function three(num) {
        const parts = num.toString().split('.');
        parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
        return parts.join('.');// '$' +
    }
 
參考網址
點我前往
點我前往

2014年10月23日 星期四

Javascript 正規表示法

之前web from時代都是用c#功能來做
網路上能找到一堆已經寫好的表示法如驗證身份證...
所以也只是照著copy沒有很懂
最近開始用比較前端的方式去開發程式
所以JS變的特別重要
看了兩篇文章後發覺好像懂了正規表達如何寫了
以下圖片來自參考網址

以上備忘改天比較好查
參考網址
點我前往
點我前往

2014年5月12日 星期一

2013年11月17日 星期日

Javascript 秒數轉時分秒

最近常在網頁上用到
尤其是撥html5 video
   function formatSecond(secs) {          
            var hr = Math.floor(secs / 3600);
            var min = Math.floor((secs - (hr * 3600)) / 60);
            var sec = parseInt( secs - (hr * 3600) - (min * 60));

            while (min.length < 2) { min = '0' + min; }
            while (sec.length < 2) { sec = '0' + sec; }
            if (hr) hr += ':';
            return hr + min + ':' + sec;

        }
參考網址 點我

2013年9月25日 星期三

Javascript return confirm 換行

雖然這是個很簡單的東西
但我始終是記不起來阿
Button1.OnClientClick = "return confirm('您確定要刪除嗎??\\n\\r刪除後將無法復原!!');";
重點再於一個\\n\\r
在程式中為什麼要\\呢如果在網頁內就直接寫\n\r即可
因為\是跳脫字元

2013年8月22日 星期四

Javascript 目前取得clinet端日期時間

以下做法雖然很瞎但是相當好用
但注意的是取得的是目前clinet端瀏覽器的時間喔非server上的時間要注意就好

var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var hour = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var NowDate = d.getFullYear() + '-' + toTen(month) + '-' + toTen(day);
var Now = d.getFullYear() + '-' + toTen(month) + '-' + toTen(day) + ' ' + toTen(hour) + ":" + toTen(minutes)+":" +toTen(seconds);
alert(NowDate);
alert(Now);
function toTen(s) {
return s < 10 ? '0' + s : s;
}

2013年5月5日 星期日

Javascript 為了再次顯示網頁,網頁瀏覽器必需重新傳送您之前送出的資訊。



<script>alert('完成');location.reload();</script>

在IE上面出現了"完成"後再跳
為了再次顯示網頁,網頁瀏覽器必需重新傳送您之前送出的資訊。
然後網頁就會一直重複
找到的解決方法為

<script>alert('完成'); location.replace(location.href);</script>

參考

點我前往
點我前往

2011年11月26日 星期六

Javascript 簡單的Google搜尋語法

因為最近客戶剛好需要google查詢的按紐 找了一下這樣寫很簡單吧


<script type="text/javascript">
function wopen(word) {
window.open('http://www.google.com/#hl=zh-TW&source=hp&q=' + word, 'Google');
                     }
</script>
 
 <input class="submit" onclick="wopen(this.form.text.value);return true;" type="submit" value="Google搜尋">
<input id="text" name="keyword" type="text">