简体   繁体   English

Javascript 转换为 OData 的 EDM 日期

[英]Javascript Convert to EDM Date for OData

How do I convert a Javascript date to EDM Date format for OData usage?如何将 Javascript 日期转换为 EDM 日期格式以供 OData 使用?

let currentDate = new Date();
// is there a native function to convert?

The following answers seem to use outdated answers, wondering if以下答案似乎使用过时的答案,想知道是否

Latest Javascript in ECMA 2020 have a native function to convert? ECMA 2020 中最新的 Javascript 有一个原生 function 可以转换吗?

https://stackoverflow.com/a/14405868/14727392 https://stackoverflow.com/a/14405868/14727392

https://stackoverflow.com/a/17284978/14727392 https://stackoverflow.com/a/17284978/14727392

Resources: https://www.odata.org/documentation/odata-version-2-0/json-format/资源: https://www.odata.org/documentation/odata-version-2-0/json-format/

Note: conducting work in Angular 10 framework注意:在 Angular 10 框架中进行工作

By the way, This function does not work in Angular giving error below,顺便说一句,这个 function 在 Angular 中不起作用,给出以下错误,

https://stackoverflow.com/a/14405868/14727392 https://stackoverflow.com/a/14405868/14727392

function convertJSONDate(jsonDate, returnFormat) {
    var myDate = new Date(jsonDate.match(/\d+/)[0] * 1);
    myDate.add(4).hours();  //using {date.format.js} to add time to compensate for timezone offset
    return myDate.format(returnFormat); //using {date.format.js} plugin to format :: EDM FORMAT='yyyy-MM-ddTHH:mm:ss'
}

Error: Property 'add' does not exist on type 'Date';错误:“日期”类型上不存在属性“添加”; Property 'format' does not exist on type 'Date' “日期”类型上不存在属性“格式”

Your question is unclear, but this is what I assume you are trying to achieve:您的问题尚不清楚,但这是我认为您正在努力实现的目标:

const dateJSONToEDM = jsonDate => {
  const content = /\d+/.exec(String(jsonDate));
  const timestamp = content ? Number(content[0]) : 0;
  const date = new Date(timestamp);
  const string = date.toISOString();
  return string;
};

console.log(
  dateJSONToEDM(`/Date(1609195194804)/`)
); // Outputs "2020-12-28T22:39:54.804Z"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM