jquery - Convert milliseconds string to date in javascript - Stack Overflow

There were a lot of answered questions about converting milliseconds to date format but none of them wa

There were a lot of answered questions about converting milliseconds to date format but none of them was able to solve my problem.

I have a string (and not a time) ing in my javascript code. It is of the format as below

1380549600000+1000

When I try to parse it using the following code it gives me "invalid date" error.

My main objective is to convert this string to dd/mm/yyyy format. So was thinking of converting it into date and applying methods like "getMonth", etc

<script>
    var modDate = "1380549600000+1000"; //Note the value is in "" hence a string
    var d = new Date(modDate); //Invalid date error here
    document.getElementById("demo").innerHTML = d;
</script>

The following works just fine. But this is not the format I am getting.

<script>
    var modDate = 1380549600000+1000; //Note the value is no longer in ""
    var d = new Date(modDate); //No problems here
    document.getElementById("demo").innerHTML = d;
</script>

Please help. Thanks in advance.

Cheers.

There were a lot of answered questions about converting milliseconds to date format but none of them was able to solve my problem.

I have a string (and not a time) ing in my javascript code. It is of the format as below

1380549600000+1000

When I try to parse it using the following code it gives me "invalid date" error.

My main objective is to convert this string to dd/mm/yyyy format. So was thinking of converting it into date and applying methods like "getMonth", etc

<script>
    var modDate = "1380549600000+1000"; //Note the value is in "" hence a string
    var d = new Date(modDate); //Invalid date error here
    document.getElementById("demo").innerHTML = d;
</script>

The following works just fine. But this is not the format I am getting.

<script>
    var modDate = 1380549600000+1000; //Note the value is no longer in ""
    var d = new Date(modDate); //No problems here
    document.getElementById("demo").innerHTML = d;
</script>

Please help. Thanks in advance.

Cheers.

Share asked Jun 9, 2015 at 12:30 samarsamar 5,2119 gold badges49 silver badges72 bronze badges 9
  • What are you trying to say by +1000? – leopik Commented Jun 9, 2015 at 12:32
  • If you remove the totally invalid +1000 and parse it as a number, it should work fine – adeneo Commented Jun 9, 2015 at 12:32
  • Use eval() to evaluate and then use your date logic, because + will be considered as illegal character if you try to parse it. – Nikhil Batra Commented Jun 9, 2015 at 12:35
  • 1 What is that format? @leopik If it is a timezone, it's a +1s timezone 0_o – CodingIntrigue Commented Jun 9, 2015 at 12:35
  • So you have to check if there is a non-numeric caracter in you string, if yes, remove the following caracters (numbers) after this non-numeric, get the result to be formatted in date. – Anwar Commented Jun 9, 2015 at 12:35
 |  Show 4 more ments

4 Answers 4

Reset to default 3

edit:-

eval is not the best approach, it is not safe to use eval, so use this instead:-

var modDate = "1380549600000+1000"
var temp = modDate.split("+");
modDate = parseInt(temp[0]) + parseInt(temp[1]);

I am not sure if you need that added 1000, if you don't, it could be done in one line as :-

modDate = parseInt(modDate.split("+")[0])

older approach :-

<script>
var modDate = eval("1380549600000+1000"); //Note the value is in "" hence a string
var d = new Date(modDate); //Invalid date error here
document.getElementById("demo").innerHTML = d;
</script>

Other approach w/o using eval:

var modDate = "1380549600000+1000";
var d = new Date(modDate.split("+")
    .map(parseFloat)
    .reduce(function(a,b){return a + b;}));

Use parseInt to get the numeric value of the string (safer than eval, but same premise):

modDate = (isNaN(modDate)) ? parseInt(modDate, 10) : modDate;

if !isNaN(modDate) {
    var d = new Date(modDate);
    document.getElementById("demo").innerHTML = d;
} else {
    console.log("Value in modDate not a number");
}

I had to use a bit of a mishmash of the answers here to get mine to work. My Date value was being sent to my web page as a String, like so: "/Date(978278400000-0500)/"

So I parsed it like this, to get it to display as a valid date:

// sDateString = "/Date(978278400000-0500)/";
 var modDate = sDateString.replace(/[\/Date\(\)]/g, "");
 return new Date(parseInt(modDate, 10));
//returns: Sun Dec 31 2000 11:00:00 GMT-0500 (Eastern Standard Time) {}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913436a4600689.html

相关推荐

  • jquery - Convert milliseconds string to date in javascript - Stack Overflow

    There were a lot of answered questions about converting milliseconds to date format but none of them wa

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信