简体   繁体   English

Javascript-如何删除字符串的一部分并将其分配给变量以进行操作? 琴弦变化

[英]Javascript - How can I remove portion of a string and assign it to a variable for manipulation? String is varied

I have an API call that returns "/Date(1425715200000)/" as part of a JSON object. 我有一个API调用,它返回“ / Date(1425715200000)/”作为JSON对象的一部分。 I need to be able to evaluate this and return the human readable date. 我需要能够对此进行评估并返回人类可读的日期。 The API documentation is scant at best. API文档充其量是很少的。 I am not even certain why they don't just store the time in milliseconds as an integer. 我什至不确定为什么他们不只是将毫秒存储为整数。

Not sure what the best way to convert and evaluate /Date(1425715200000)/ this to output 3/7/15. 不知道什么是转换和评估/ Date(1425715200000)/从而输出3/7/15的最佳方法。

Used this to replace the "/" characters. 用它代替“ /”字符。

x = x.replace(/\//g,'');

You will have to add in custom code to parse your time in milliseconds dates and create a new Date() object from it. 您将必须添加自定义代码,以毫秒为单位解析您的时间,并从中创建一个新的Date()对象。 Then extract the month day and year properties of the date object. 然后提取日期对象的月日和年属性。

Heads up on the .getMonth as it will return a value from 0-11. 前往.getMonth,因为它将返回0-11之间的值。

 var jsonObj = { date :"/Date(1425715200000)/" } function parseDateString( dateObjString ){ var startIndex = dateObjString.indexOf("(") + 1; var endIndex = dateObjString.indexOf(")"); var timeInMilli = dateObjString.substring( startIndex, endIndex); var displayDate; var date = new Date(); date.setTime( timeInMilli ); displayDate = ( date.getMonth() + 1 ) + "/" + date.getDate() + "/" + date.getFullYear(); alert( displayDate ); } parseDateString( jsonObj.date ); 

With this sample, you will retrieve a Date instance that you can display as you want: 通过此示例,您将检索一个Date实例,可以根据需要显示该实例:

 var result = "/Date(1425715200000)/"; result = result.replace(/\\/Date\\((\\d*?)\\)\\//, "$1"); result = new Date(1 * result); document.getElementById("output").textContent = result; 
 <span id="output"></span> 

Use regex to extract the timestamp, then create an instance of the Date class. 使用正则表达式提取时间戳,然后创建Date类的实例。

 var result = { date: "/Date(1425715200000)/" }; var ts, parts = result.date.match(/\\/Date\\(([0-9]+)\\)\\//); if (parts && parts.length == 2) { ts = parseInt(parts[1]); } if (ts) { var date = new Date(ts); var str = (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear(); console.log(str); document.getElementById("output").textContent = str; } 
 <span id="output"></span> 

A little more detail to my comment... 我的评论有更多细节...

var test = "\\Date(1425715200000)\\";
var time = test.match(/\d+/)[0];
var intTime = parseInt(time);
var date = new Date(intTime);
console.log(date); // Sat Mar 07 2015 02:00:00 GMT-0600 (CST)
console.log((date.getUTCMonth() + 1) + "/" + date.getUTCDate() + "/" + date.getUTCYear()); // 03/07/14

Try the following: 请尝试以下操作:

var dt = new Date(parseInt('/Date(1425715200000)/'));
var month = dt.getMonth()++, date = dt.getDate(), year = dt.getYFulllYear();
console.log(month+'/'+date+'/'+year.substr(2));

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

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