Last active
August 29, 2015 14:14
-
-
Save wongsyrone/7df8f0dda173ede7075b to your computer and use it in GitHub Desktop.
计算四班三运转(C-sharp2JavaScript)
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Common | |
{ | |
public static class GetShift | |
{ | |
public static string Shift(string input_date) | |
{ | |
int var_day_num; | |
int var_num; | |
string shift_no="甲"; | |
DateTime dtNightEnd = Convert.ToDateTime(input_date); //获取输入时间日期转成DateTime结构 | |
/* | |
* DateTime struction: | |
* | |
* | |
* */ | |
TimeSpan ts = dtNightEnd.Subtract(Convert.ToDateTime("1991-12-13")); | |
var_day_num = ts.Days; // 求与指定Date间隔的天数,该方法能精确到 millisecond | |
var_num = var_day_num % 8; // 天数对8取余 | |
DateTime ShortTime = Convert.ToDateTime(dtNightEnd.ToShortTimeString()); //获取输入DateTime的时间短格式 aka先转为短时间字符串:1:45 PM再转回DateTime结构 | |
//时间比较 | |
DateTime dt00 = Convert.ToDateTime("00:00:00"); | |
DateTime dt08 = Convert.ToDateTime("08:00:00"); | |
DateTime dt16 = Convert.ToDateTime("16:00:00"); | |
DateTime dt23 = Convert.ToDateTime("23:59:59"); | |
if (var_num == 0) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "乙"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "丙"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "丁"; | |
} | |
} | |
if (var_num == 1) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "甲"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "丙"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "丁"; | |
} | |
} | |
if (var_num == 2) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "甲"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "乙"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "丙"; | |
} | |
} | |
if (var_num == 3) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "丁"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "乙"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "丙"; | |
} | |
} | |
if (var_num == 4) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "丁"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "乙"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "甲"; | |
} | |
} | |
if (var_num == 5) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "丙"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "甲"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "乙"; | |
} | |
} | |
if (var_num == 6) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "丙"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "丁"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "甲"; | |
} | |
} | |
if (var_num == 7) | |
{ | |
if (ShortTime >= dt00 || ShortTime < dt08) | |
{ | |
shift_no = "乙"; | |
} | |
if (ShortTime >= dt08 || ShortTime < dt16) | |
{ | |
shift_no = "丁"; | |
} | |
if (ShortTime >= dt16 || ShortTime < dt23) | |
{ | |
shift_no = "甲"; | |
} | |
} | |
return shift_no; | |
} | |
} | |
} |
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
// calc Date Diff | |
// input string format: 2014-12-12 08:00:00 | |
// date pattern: (\d\d\d\d)[-./](1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01]) | |
// 年 月 日 分隔符为 -./ 前缀0可有可无 | |
// Time pattern: 00:00:00 - 23:59:59 | |
// (0?\d|1\d|2[0-3]):(0?\d|[1-5]\d):(0?\d|[1-5]\d) | |
// 24小时制 时分秒 前缀0可省略 | |
// update1 : 连接字符串使用DateTime格式初始化Date对象,通过parse返回毫秒数进行比较,未测试 | |
// update2 : 使用毫秒数进行比较,比较之前必须进行取整(使用的是向下取整) | |
// 日期正则也有问题,采用最简单的匹配形式了 | |
function Main() { // using milliseconds to compare | |
var strInput = "2012-12-12 08:00:00"; | |
var shiftNO = ""; | |
var pattDate = /(\d\d\d\d)[-./](\d\d)[-./](\d\d)/g; | |
//var pattTime = /(\d\d):(\d\d):(\d\d)/g; | |
var end = "1991-12-13 00:00:00"; | |
var startTime = new Date(strInput.replace(/-/g, "/")).getTime(); | |
var endTime = new Date(end.replace(/-/g, "/")).getTime(); | |
var dates = Math.floor(Math.abs(startTime - endTime) / (1000*60*60*24)); // 向下取整 | |
var intNum = dates % 8; | |
var tmpDate = strInput.match(pattDate); // object | |
var str00 = tmpDate[0] + " 00:00:00"; // tmpDate[0] is 'string' | |
var str08 = tmpDate[0] + " 08:00:00"; | |
var str16 = tmpDate[0] + " 16:00:00"; | |
var str23 = tmpDate[0] + " 23:59:59"; | |
var dtInput = new Date(strInput.replace(/-/g, "/")).getTime(); | |
var dt00 = new Date(str00.replace(/-/g, "/")).getTime(); | |
var dt08 = new Date(str08.replace(/-/g, "/")).getTime(); | |
var dt16 = new Date(str16.replace(/-/g, "/")).getTime(); | |
var dt23 = new Date(str23.replace(/-/g, "/")).getTime(); | |
if (intNum === 0) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "b"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "c"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "d"; | |
} | |
} | |
if (intNum == 1) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "a"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "c"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "d"; | |
} | |
} | |
if (intNum == 2) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "a"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "b"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "c"; | |
} | |
} | |
if (intNum == 3) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "d"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "b"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "c"; | |
} | |
} | |
if (intNum == 4) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "d"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "b"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "a"; | |
} | |
} | |
if (intNum == 5) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "c"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "a"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "b"; | |
} | |
} | |
if (intNum == 6) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "c"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "d"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "a"; | |
} | |
} | |
if (intNum == 7) | |
{ | |
if ((dtInput >= dt00) || (dtInput < dt08)) | |
{ | |
shiftNO = "b"; | |
} | |
if ((dtInput >= dt08) || (dtInput < dt16)) | |
{ | |
shiftNO = "d"; | |
} | |
if ((dtInput >= dt16) || (dtInput < dt23)) | |
{ | |
shiftNO = "a"; | |
} | |
} | |
// tested via statement below | |
//document.getElementById("demo").innerHTML = shiftNO; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: