1

i am having the time in "2016-11-17T09:22:24Z" and i need to convert it into "2016-11-1709:22:24".Just need to remove T and Z from solr Date and i need to add 330 minutes to that date and display it

Sanket Patel
  • 901
  • 9
  • 21

2 Answers2

1

First we can construct a javascript date object from the string and then you can convert it into correct format:

var dt = new Date('2016-11-17T09:22:24Z')
var formattedDate = dt.toISOString().substring(0, 19).replace('T', '')

console.log(formattedDate) should log 2016-11-1709:22:24

Or in one line:

new Date('2016-11-17T09:22:24Z').toISOString().substring(0, 19).replace('T', '')

will render: 2016-11-1709:22:24

Some more good discussions here: Convert javascript to date object to mysql date format (YYYY-MM-DD)

Community
  • 1
  • 1
Ahsan
  • 3,845
  • 2
  • 36
  • 36
  • JavaScript does not really have a specific date format. Your current Solr date format is good enough for Javascript to understand. For example: `new Date('2016-11-17T09:22:24Z')` will give a JavaScript date. – Ahsan Dec 01 '16 at 07:09
  • but when i am converting formatted date into date it is showing invalid date – Sanket Patel Dec 01 '16 at 07:12
  • new date(formattedDate); this shows the error of invalid date – Sanket Patel Dec 01 '16 at 07:13
  • `formattedDate` is already a date object. You do not have to convert it. I am updating the answer a bit to show you a one liner solution in case it helps you. – Ahsan Dec 01 '16 at 07:25
  • i also need to add 330 mins in it how can i add it? – Sanket Patel Dec 01 '16 at 08:13
  • just run: `dt.setMinutes(dt.getMinutes() + 330)` and now `dt` should have 330+ mins – Ahsan Dec 01 '16 at 08:23
  • i was doing the same thing but set and get method is not supported to that dt object. it shows the error that object does not support the getMinutes method – Sanket Patel Dec 01 '16 at 09:44
  • thank you for the help i got the output from the the below code – Sanket Patel Dec 01 '16 at 10:24
  • var publish = new Date("2016-11-17T09:22:24Z"); var min = publish.setMinutes(publish.getMinutes() + parseInt(330)); var dt = new Date(min); alert(dt); – Sanket Patel Dec 01 '16 at 10:24
1
var userdate = new Date("2009-1-1T8:00:00Z");
var timezone = userdate.getTimezoneOffset();
var serverdate = new Date(userdate.setMinutes(userdate.getMinutes()+parseInt(timezone)));

This will give you the proper UTC Date and Time. It's because the getTimezoneOffset() will give you the timezone difference in minutes. I recommend you that not to use toISOString() because the output will be in the string Hence in future you will not able to manipulate the date

Sanket Patel
  • 901
  • 9
  • 21