-5

/Date(1396505647431)/

  1. How to extract only Date(1396505647431) string from above string using javascript?.
  2. How to convert Date(1396505647431) string into DateAndTime using javascript?
  3. How to extract time portion (like 10:00 AM) of the above converted date using Javascript.
Constantin
  • 8,721
  • 13
  • 75
  • 126

3 Answers3

1

Use the string.prototype.match method:

var str = "/Date(1396505647431)/";
  1. str.match(/Date\(\d+\)/)[0] gives "Date(1396505647431)"

  2. var time = new Date(+str.match(/\d+/)[0]) gives Thu Apr 03 2014 11:44:07 GMT+0530 (India Standard Time)

  3. time.toLocaleTimeString() gives "11:44:07 AM"

DEMO

tewathia
  • 6,890
  • 3
  • 22
  • 27
0

I am assuming that you are looking for timestamp in /Date(1396505647431)/ string. You can use substring and indexOf functions. if its follow same format alwayz

1. var ts = str.substring(str.indexOf('(')+1,str.indexOf(')'));

1396505647431

2. var d = Date(ts);

Thu Apr 03 12:07:12 2014

3.new Date(ts).toLocaleTimeString();

11:44:07 AM
SK.
  • 4,174
  • 4
  • 30
  • 48
-1

//if it is unix timestap....

var date = new Date(unix_timestamp*1000);

var hours = date.getHours();

var minutes = date.getMinutes();

var seconds = date.getSeconds();

var formattedTime = hours + ':' + minutes + ':' + seconds;

for complete reference .. Convert a Unix timestamp to time in JavaScript

Community
  • 1
  • 1
Ram Chowdary
  • 53
  • 2
  • 13