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
Asked
Active
Viewed 747 times
1
-
As per the title of the question, he actually needs a Javascript date, right? – Ahsan Dec 01 '16 at 06:50
2 Answers
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)
-
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
-
-
`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
-
-
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
-
-
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