【问题标题】:Date returning from c# web method - /Date(xxxxxxx)/从 c# web 方法返回的日期 - /Date(xxxxxxx)/
【发布时间】:2016-01-27 11:13:07
【问题描述】:

我正在使用 jquery ajax 通过 c# web 方法从数据库请求对象列表:

[WebMethod]
public static List<SessionQueue> GetActiveIssues()
{           
  try
  {
    return MyContext.SessionQueues.Where(x => !x.Resolved).OrderBy(d => d.SubmittedTime).ToList();
  }
  catch (Exception ex)
  {
    throw new Exception(ex.ToString()); // catch in jquery
  }   

}

每个SessionQueue 对象都有一个属性:

public System.DateTime SubmittedTime

当我在 jquery 中收到 SubmittedTime 时,日期如下所示:

/Date(1445975227197)/

如何将其解析为有效的日期字符串?即mm/dd/yy xx:xx am

我试过了:

function formatLongDate(date) {

    var nd = Date.parse(date);
    var dt = new Date(nd);
    var dtStr = $.datepicker.formatDate("mm/dd/yy", dt) + ' ' + getTime(date)
    return dtStr;
}

/Date(1445975227197)/ 不是有效的日期标记

【问题讨论】:

  • 我相信这是自 1970 年 1 月 1 日以来的滴答数。
  • 使用moment.js。我也有类似的情况,momentjs 帮了我很多。
  • 或者切换到使用 JSON.NET 4.5+ 作为默认以 ISO 8601 格式格式化日期的序列化程序。

标签: c# jquery ajax date webmethod


【解决方案1】:

尝试使用String.prototype.match()Array.protottpe.map()

new Date("/Date(1445975227197)/".match(/\d+/).map(Number)[0])

【讨论】:

  • 如何以“2015 年 10 月 28 日”等格式获取输出?
  • @Arun String(new Date("/Date(1445975227197)/".match(/\d+/).map(Number)[0])).slice(4, 15)
  • 谢谢@guest271314
【解决方案2】:

MVC 将日期转换为从 1970 年 1 月 1 日开始的毫秒数。将其转换为 javascript 日期的最简单方法是执行以下操作

var convertedDate = new Date(parseInt(SubmittedTime.substr(6)));

【讨论】:

    【解决方案3】:

    Calendar calendar = Calendar.getInstance();
    //timestamp is where you're getting the 1445975227197
    calendar.setTimeInMillis(timeStamp);
    
    int mYear = calendar.get(Calendar.YEAR);
    int mMonth = calendar.get(Calendar.MONTH);
    int mDay = calendar.get(Calendar.DAY_OF_MONTH);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      相关资源
      最近更新 更多