需求
客户端要统计每一年每一周的盈亏记录
思路
- 客户端请求:
如果本地没有记录则发送给服务器当前时间,服务器去计算最新一周的数据,如果本地有记录则发送给服务器当前一周第一天的日期 - 服务器响应:
服务器返回年、周、这一周的日期以及每一天的盈亏记录和这一周的总赢亏记录
测试代码
日期相关的处理代码
var time = DateTime.Now;
var year = time.Year;
string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
int weeknow = Convert.ToInt32(time.DayOfWeek);
string week = Day[Convert.ToInt32(weeknow.ToString("d"))].ToString();
GregorianCalendar gc = new GregorianCalendar();
int weekOfYear = gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
int daydiff = (-1) * weeknow;
string FirstDay = time.AddDays(daydiff).ToString("yyyy-MM-dd");
Debug.LogError(string.Format("当前时间:{0} 是{1}年 第{2}周 {3} 这一周的第一天日期:{4}", time, year, weekOfYear, week, FirstDay));
var month = time.Month;
var day = time.Day;
Debug.LogError(string.Format("日期:{0:00}.{1:00}", month, day));
DateTime startWeek = time.AddDays(1 - Convert.ToInt32(time.DayOfWeek.ToString("d"))); //本周周一
DateTime endWeek = startWeek.AddDays(6); //本周周日
Debug.LogError(string.Format("本周第一天:{0} 最后一天:{1}", startWeek, endWeek));
DateTime nexeWeekFirstDay = startWeek.AddDays(-7);
Debug.LogError(string.Format("上一周的第一天:{0}", nexeWeekFirstDay));
结果
备注
去掉小时分钟,直接DateTime对象.Date属性只会保留日期,时分秒默认都会置0