asp.net mvc中的日期时区转换

时间:2016-07-26 05:11:21

标签: c# datetime asp.net-mvc-5 data-conversion

我是C#的新手,我需要帮助。

我正在写一个考勤应用程序,它在localhost上工作得很好。

当我在www.gearhost.com上发布它时,云托管可能会使用MD时间(山区时间),当我在www.gearhost.com上发布时,它会显示不同的员工和不同的时间显示AM到PM 。

这是我的方法GetEmployeeToday,它显示员工进出时间码。

public IQueryable<EmptodayModel> GetEmployeeToday()
{  
    obj = new EmployeeEntities();

    TimeZoneInfo timeZoneInfo;
    // Set the time zone information from US Mountain Standard Time to Pakistan Standard Time
    timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");

    // Get date and time in US Mountain Standard Time 
    DateTime now = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);

    var employeetoday = from atd in obj.tbl_Attandence
                        join e in obj.tbl_Employee on atd.Eid equals e.Eid
                        where (DbFunctions.TruncateTime(atd.IN) == DbFunctions.TruncateTime(now))
                        select new EmptodayModel
                                {
                                    AID = atd.AID.ToString(),
                                    Eid = atd.Eid.ToString(),
                                    EName = e.EName,
                                    IN = (DateTime?)atd.IN,
                                    OUT = (DateTime?)atd.OUT
                                };

    return employeetoday;
}

我还将员工插入日期时间 - 这是代码:

public ActionResult EmployeeIN(DateTime IN, string e)
{
    TimeZoneInfo timeZoneInfo;

    // Set the time zone information to US Mountain Standard Time 
    timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");

    // Get date and time in US Mountain Standard Time 
    now = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);

    int Count = obj.attendance.GetAll().Where(x => x.IN.Value.Date == now.Date && x.Eid == Convert.ToInt32(e)).Count();

    if (IN != null || e != null)
    {
        if (Count <= 0)
        {
            att.IN = TimeZoneInfo.ConvertTime(IN, timeZoneInfo);
            att.Eid = Convert.ToInt32(e);
            obj.attendance.Insert(att);
            e = e + " Employee successfully IN";
        }
        else
        {
            e = e + " Employee already IN";
        }
    }

    return Json(e, JsonRequestBehavior.AllowGet);
    //obj.attendance.Insert(att);
    //return Json(att, JsonRequestBehavior.AllowGet);
}

这里是员工输出代码

[HttpPost]
public ActionResult EmployeeOUT(DateTime OUT, string Eid)
{
    TimeZoneInfo timeZoneInfo;

    // Set the time zone information to US Mountain Standard Time 
    timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");

    // Get date and time in US Mountain Standard Time 
    now = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);

    if (OUT != null || Eid != null)
    {
        List<tbl_Attandence> data = new List<tbl_Attandence>();
        string time = OUT.ToString("dd/MM/yyyy HH:mm:ss");

        DateTime n = DateTime.ParseExact(time, "dd/MM/yyyy HH:mm:ss", new CultureInfo("en-US"));
        data = obj.attendance.GetAll().Where(x => x.IN.Value.Date == now.Date && x.Eid == Convert.ToInt32(Eid)).ToList();

        int aid = data.FirstOrDefault().AID;
        att = obj.attendance.GetById(aid);
        int Count = data.Count;

        if (Count > 0)
        {
            att.OUT = TimeZoneInfo.ConvertTime(n, timeZoneInfo);
            obj.attendance.Update(att);
        }
    }

    return Json( JsonRequestBehavior.AllowGet);
}

0 个答案:

没有答案