2

I have Fullcalender's events which are return date as String format it's like this 'Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)' I want to convert this to javascript date exactly the same format Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time).because I want to use this method toDateString() to check some conditions.

NicoleZ
  • 1,485
  • 3
  • 22
  • 43
  • `new Date('Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)')` – Yahiya Oct 23 '19 at 09:49
  • *real javascript date exactly the same format* ... *real* javascript Date's are actually stored as a number of milliseconds since jan 1 1970 - so, that statement is impossible - though as stated, `new Date` is what you use – Bravo Oct 23 '19 at 09:56
  • `I want to use this method toDateString() to check some conditions` sure, you have a comment that has the answer, and an answer already - not sure why you want to clarify – Bravo Oct 23 '19 at 10:17
  • to use `toDateString()` needs a date object. for that I want it to convert my whole string to a date. I got the answer below thanks for your concern. – NicoleZ Oct 23 '19 at 10:21
  • Why do you want to convert a string to a Date so that you can generate another string with exactly the same values as the original string? That really doesn't make any sense. It is only guaranteed to return the same values for *toDateString* where the host system is set to India Standard Time, any other setting will likely produce different values depending on system settings. – RobG Oct 23 '19 at 10:52
  • @Robg Because I have string date and date object I need to compare `Wed Oct 23 2019` part of the date – NicoleZ Oct 23 '19 at 10:55
  • Your question still doesn't make any sense. You haven't shown what you are actually trying to do, and there are many, many questions already on how to parse a string to a Date (where the built–in parser is usually the least reliable method). – RobG Oct 23 '19 at 10:58
  • yeah before I post this read some of them. the thing I have a date which is return form full calendar as a string but formatted as "'Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)"` i have another date object which is type date.so what I want is compare those two. for that, I have to convert that string date to a date type object with exact format.that's the question I had. – NicoleZ Oct 23 '19 at 11:32
  • Searching on [\[javascript\]\[date\]](https://stackoverflow.com/search?q=%5Bjavascript%5D+convert+string+to+date) returns over 3,700 results, the most voted answer has 581 votes and 41 answers, a number of which suggest `new Date(string)`, along with many other useful details not included here. There are also many questions and answers on [how to compare two dates](https://stackoverflow.com/search?q=%5Bjavascript%5D+compare+two+dates). – RobG Oct 23 '19 at 12:22

1 Answers1

3

From a string to Date type :

const dateStr = 'Wed Oct 23 2019 00:00:00 GMT+0530 (India Standard Time)';
const dateObj = new Date(dateStr) // insert your date variable here
Tiago Silva
  • 2,299
  • 14
  • 18