2018年5月31日 星期四

c# 判斷日期區間遇到幾次生日

最近用到寫的日期區間遇到幾次生日
也可以用來判斷日期區遇到日期幾次
也許有更好的可以交流
public static int GetBetweenBirthdayCount(DateTime pFrom, DateTime pTo, DateTime pBirthday)
        {
            int mBirthdayCount = 0;//會過幾次生日

            int TotalYear = pTo.Year - pFrom.Year;//看區間有幾年

            for (int i = 0; i <= TotalYear; i++)
            {

                int y = pFrom.AddYears(i).Year;
                string m = pBirthday.Month.ToString().PadLeft(2, '0');
                string d = pBirthday.Day.ToString().PadLeft(2, '0');
               
                //如果他是生日229的要判斷
                if (m == "02" && d == "29")
                {//四除的盡就算閏年
                    if (y % 4 != 0)
                    {
                        d = "28";
                    }
                }


                DateTime EveryBirthday = DateTime.Parse(y + "-" + m + "-" + d);

                if (pFrom <= EveryBirthday && pTo >= EveryBirthday)
                {
                    mBirthdayCount++;
                }
            }

            return mBirthdayCount;
        }

2018年5月24日 星期四

Angular 安裝時常用問題處理(09)

1.You seem to not be depending on "@angular/core" . This is an error.
有時候重裝就可以了
npm cache clear --force
npm install
2.Cannot find module '@angular-devkit/core' ...
 
npm install --save-dev @angular-devkit/build-angular
3.刪除node_modules資料夾再重新
npm install  
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2018年5月21日 星期一

Angular [(Two Way Binding)](雙向繫結)(08)

html部分
<input [(ngModel)]="keyword" placeholder="[(雙向繫結)]" (keyup.escape)="onKeyClear()"/>
  文字:{{keyword}},字數:{{keyword.length}}
ts部分
  keyword = '';
  onKeyClear() {
    this.keyword = "";
  }
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular (Event Binding)(事件繫結)(07)

$event可傳可不傳
html部分
<a on-click="CountPlus($event)">點擊次數{{count}}</a><br />
<a (click)="CountPlus($event)">點擊次數{{count}}</a>
ts部分
export class EventBindingComponent implements OnInit {
  count = 0;
  CountPlus($event:any) {
    this.count++;
    console.log($event);
  }
  constructor() { }
  ngOnInit(): void {
  }
}
===============================================================
保哥作業1
請利用 Angular 資料繫結方法 (內嵌繫結、屬性繫結、事件繫結) 完成任務。
1.當使用者在網頁右側的關鍵字搜尋輸入文字時,會自動在輸入框的正下方顯示正在輸入的字元數。
2.當使用者在輸入框按下 Escape 按鍵時,會自動清空輸入框的內容。
2022-09-22修正,比較懶的做法寫在同一個判斷中。
html部分
  <input type="text"
         (keyup)="onKeyClear($event)" />
  字數:{{keywordCount}}
ts部分
  keyword = "";
  keywordCount = 0;
  onKeyClear(event: KeyboardEvent) {
    let elm = (<HTMLInputElement>event.target);
    if (event.key == "Escape") {
      elm.value = "";//這段要清除掉Html上的文字
      this.keyword = "";
    } else {
      this.keyword = elm.value;
    }
    console.log(this.keyword);
    this.keywordCount = this.keyword.length;
  }
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular [Property Binding](屬性繫結)(06)

html部分
<a [href]="url" [attr.data-tite]="title">{{title}}</a>
ts部分
export class PropertyBindingComponent implements OnInit {
  title = "{{屬性繫結}}";
  url = "http://tw.yahoo.com";  
  constructor() { }
  ngOnInit(): void {
  }
}

參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2018年5月18日 星期五

Angular {{Interpolation}}(內嵌繫結)(05)

html部分
 <a href="{{url}}">{{ title }}</a>
ts部分
 export class InterpolationComponent implements OnInit {
  title = "{{內嵌繫結}}";
  url = "http://tw.yahoo.com";
  constructor() {   
  }
  ngOnInit(): void {
  }
}
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2018年5月17日 星期四

Angular 如何建立component(04)

Angular每一個畫面都是多個component所組成
以下會記錄如何新增component
1.首先先輸入
ctrl + `(單引號,通常在1的左邊tab上面)
2.按+號新增終端機,預設可能是power shell,選擇終端機資料夾預設應該會在目前的專案上
3.建立component
ng generate component pages
ng generate 建立一個 簡寫 g component 哪一種範本 簡寫 cpages 頁面名稱 

未來請輸入方式如下列
ng g c pages
ng g -h 可看目前版本所有可建立的範本
4.建立起來後,會多一個資料夾及有四個檔案,然後app.module.ts 會異動
CREATE src/app/pages/pages.component.html (24 bytes) <-Html
CREATE src/app/pages/pages.component.spec.ts (621 bytes) <-測試檔
CREATE src/app/pages/pages.component.ts (265 bytes) <-程式
CREATE src/app/pages/pages.component.css (0 bytes) <-css

UPDATE src/app/app.module.ts (392 bytes) 修改了declarations
及import { PagesComponent } from './pages/pages.component';
5.如何顯示這個頁面?
找到剛新增的pages.component.ts
裡面有一個 selector: 'app-pages'
只要將app-pages加入在app.component.html即可顯示出剛剛的頁面
<app-pages></app-pages>
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular 啟動順序(03)

AngularJs只需在前端宣告ng標籤即可運行
但到了Angular的話完全不同
這次框架有自己個一個流程
所有的程式碼及html都有固定的放法
這樣程式在跑的時候才會正常運行
前篇文章所提到的方法啟動server
npm start
以下是已專案預設的程式跑起來的順序
1.進入網頁index.html
2.main.ts開始載入import
3.啟動AppModule
4.執行AppComponent元件
5.執行AppComponent class
6.執行AppComponent templateUrl
7.執行AppComponent style
8.載入完成呈現畫面
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

Angular 新增專案+開發環境設定+常用快速鍵(02)

由於使用Visual Studio Code跟之前使用Visual Studio有點不太一樣
在Visual Studio圖形界面化的方式可新增專案很方便
Visual Studio Code中可能需要打些指令來新增在此筆記一下
1.cmd
2.建立一個資料夾來放專案用,因有些公司可能在C:無法執行所以此次在D:建立一個projects資料夾,使用Angular Cli來建立Angular的專案(請使用英文或數字命名)
cd\
d:
md projects
cd projects
ng new firstapp
3.測試是否有建置成功,至剛新增的資料夾使用npm
cd firstapp
npm start
4.開啟瀏覽器看看網站是否有成功,有跑起來會有Welcome to app!
http://localhost:4200/
5.如何發佈
一般
ng build
加參數--prod
ng build --prod
壓縮整的專案js變小
===============================================================
A.如何改變npm的Port
找到package.json
"start": "ng serve"
改
"start": "ng serve --port 8080"
B.如何關閉npm Server
ctrl + c
y
C.開啟VS Code,在專案下輸入
code .
D.如果Angular版本更新了要如何升級
專案目錄下
ng update
會出現以下表示升級完成
 We analyzed your package.json, there are some packages to update:

      Name                               Version                  Command to update
     --------------------------------------------------------------------------------
      @angular/cli                       1.2.3 -> 6.0.2           ng update @angular/cli
      @angular/core                      4.4.7 -> 6.0.2           ng update @angular/core
      rxjs                               5.5.10 -> 6.1.0          ng update rxjs


    There might be additional packages that are outdated.
    Or run ng update --all to try to update all at the same time.
E.以述為單一專案升級,升級Global Agnular Cli版本方式
看目前版本
 npm list -g --depth=0
檢查最新版本
npm outdated -g
沒出現文字為目前最新版
有出現代表有新版
Package       Current  Wanted  Latest  Location
@angular/cli    6.0.1   6.0.3   6.0.3
若有新版可執行下列安裝最新版
npm install -g @angular/cli
===============================================================
1.格式化文件
shift + alt + f
2.ts html 快速切換
alt + o
3.註解
ctrl + /
===============================================================
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網

2018年5月16日 星期三

Angular 安裝開發環境For Windows(01)

之前寫過AngularJs目前,改成Angular還尚未寫過完整專案藉由此次機會,從開始使自己筆記一下
此次會使用到多次的命令提示字元,均請使用管理者Administrator開啟,之後都使用cmd代替
1.首先安裝Node JS套件管理工具
2.安裝Visual Studio Code開發工具
3.cmd(記得使用administrator開啟),看版本
npm -v
node -v
4.安裝angular cli開啟命令提示字元
npm install -g @angular/cli 
ng -v
如有安裝錯誤可輸入依序
npm uninstall -g angular-cli 
npm uninstall -g @angular/cli 
npm cache clean 
npm install -g @angular/cli 
參考資料
1.保哥的Angular 開發實戰:從零開始
2.Angular官網