Last active
August 29, 2015 14:00
-
-
Save FrankWu100/11137826 to your computer and use it in GitHub Desktop.
PCCU YYM C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
namespace CoreLib | |
{ | |
public class PCCUYYM | |
{ | |
string _YYM = ""; | |
public static PCCUYYM Now { | |
get { return new PCCUYYM (DateTime.Now); } | |
} | |
public PCCUYYM () | |
{ | |
} | |
public PCCUYYM (DateTime _DateTime) | |
{ | |
setYYM(_DateTime); | |
} | |
public PCCUYYM (string dateStr) | |
{ | |
DateTime parsedDate; | |
if (DateTime.TryParse (dateStr, null, DateTimeStyles.None, out parsedDate)) { | |
Console.WriteLine ("Converted '{0}' to {1:yyyy/MM/dd}.", dateStr, parsedDate); | |
setYYM(parsedDate); | |
} else { | |
Console.WriteLine ("Unable to convert '{0}' to a date and time.", dateStr); | |
} | |
} | |
public PCCUYYM (string pattern, string dateStr) | |
{ | |
DateTime parsedDate; | |
if (DateTime.TryParseExact (dateStr, pattern, null, DateTimeStyles.None, out parsedDate)) { | |
Console.WriteLine ("Converted '{0}' to {1:yyyy/MM/dd}.", dateStr, parsedDate); | |
setYYM(parsedDate); | |
} else { | |
Console.WriteLine ("Unable to convert '{0}' to a date and time.", dateStr); | |
} | |
} | |
void setYYM(DateTime _DateTime) | |
{ | |
DateTime dateNow = _DateTime; | |
string yyyyStr = dateNow.ToString("yyyy"); | |
string mmStr = dateNow.ToString("MM"); | |
// Console.WriteLine(@"STR - YY: " + yyyyStr + " M: " + mmStr); | |
int yyyyNum = int.Parse(yyyyStr); | |
int mmNum = int.Parse(mmStr); | |
// Console.WriteLine(@"INT - YY: " + yyyyNum + " M: " + mmNum); | |
// ex 2014 | |
if (mmNum < 2) { | |
// 上學期 | |
// ex 1021 : 2014-01-01 ~ 2014-01-31 | |
Console.WriteLine("PCCU- YY: {0} M: {1}", (yyyyNum - 1911 - 1).ToString().PadLeft(3, '0'), 1); | |
_YYM = (yyyyNum - 1911 - 1).ToString ().PadLeft (3, '0') + "1"; | |
} | |
else if (mmNum >= 2 && mmNum < 8) { | |
// 下學期 | |
// ex 1022 : 2014-02-01 ~ 2014-07-31 | |
Console.WriteLine("PCCU- YY: {0} M: {1}", (yyyyNum - 1911 - 1).ToString().PadLeft(3, '0'), 2); | |
_YYM = (yyyyNum - 1911 - 1).ToString ().PadLeft (3, '0') + "2"; | |
} | |
else if (mmNum >= 8) { | |
// 上學期 | |
// ex 2031 : 2014-08-01 ~ 2014-12-31 | |
Console.WriteLine("PCCU- YY: {0} M: {1}", (yyyyNum - 1911).ToString().PadLeft(3, '0'), 1); | |
_YYM = (yyyyNum - 1911).ToString ().PadLeft (3, '0') + "1"; | |
} | |
} | |
public string getYYM () | |
{ | |
if (string.IsNullOrEmpty (_YYM)) { | |
setYYM (DateTime.Now); | |
} | |
return _YYM; | |
} | |
public string getYY () | |
{ | |
return getYYM ().Substring(0, 3); | |
} | |
public string getM () | |
{ | |
return getYYM ().Substring(3, 1); | |
} | |
public string YYM { | |
get { | |
if (string.IsNullOrEmpty (_YYM)) { | |
setYYM (DateTime.Now); | |
} | |
return _YYM; | |
} | |
} | |
public string YY { | |
get { return YYM.Substring(0, 3); } | |
} | |
public string M { | |
get { return YYM.Substring(3, 1); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment