{"date":"Thu Dec 06 14:56:01 IST 2012"}
I am getting this string as JSON can I convert it to JS date object?
Edit: Unfortunately i was totally wrong, sry for that,my bad, it happened to always result in today,
but to not screw you up, heres an solution which should work for you anyway
If you get Different Time strings from your Server, maybe the best way is to write a Regex pattern that matches your String patterns
Access your date propertie from your JSON Object
Since instantiating a Date object with this "Thu Dec 06 14:56:01 IST 2012" String would result in an Invalid Date
Remove the "IST" myJson.date.replace(" IST","")
Instantiate the your Date object with your new String myDate = new Date("Thu Dec 06 14:56:01 2012")
Now theres really your Date Object
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2012"}
var myDate = new Date(myJson.date.replace(" IST",""))
console.log(myDate.toLocaleDateString())
Heres the JSBin
The right way to convert your JSON to the data object it's parsing this date as a string.
var myJson = {"date":"Thu Dec 06 14:56:01 IST 2013"}
var myDate = new Date(Date(myJson.date))
console.log(myDate.getFullYear()) // 2012
Doesn't work with a Year different from the current one.
Related link
Where can I find documentation on formatting a date in JavaScript?
Related
I am working in Firebase and writing a Cloud Function in JavaScript. I am trying to extract a Month Day, Year string from a Firebase Timestamp (from my database).
In my Cloud Function I use timestampObj.toDate().toString() and it returns a string like this in my client-side:
Fri Dec 06 2019 18:06:52 GMT+0000 (UTC)
How do I get the string December 6, 2019 from the line above in my Dart code?
Converting the DateTime returned by toDate() into a String sounds like putting yourself through much more trouble than necessary. DateTime has lots of methods for easily extracting the calendar components out of it. You can see from that API doc link that it has properties for year, month, and day that require no text parsing. It should be trivial to convert those into the string format you want.
Even better, you can just use the DateFormat class.
Will this help? Shouldn't the output be "December 6, 2019" instead of December 9?
var date = new Date("Fri Dec 06 2019 18:06:52 GMT+0000 (UTC)"); //
var result = date.toLocaleString('default',{month:'long'})+" "+date.getDate()+", "+date.getFullYear();
console.log(result); // "December 6, 2019"
In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?
It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.
I am looking to parse the below date format in JavaScript but I am struggling to make it work reliably.
//Works
var a = new Date("Thu, Nov 7 12:59:45 GMT 2013")
alert(a)
//Invalid date
var b = new Date("Tue, Jun 19 11:14:23 BST 2012")
alert(b)
What is a reliable method of parsing this date format?
I am testing in FireFox 36.
Found that the parsing will automatically associate the timezone if I remove it
var b = new Date("Tue, Jun 19 11:14:23 BST 2012".replace("BST",""))
I would prefer it to just work but meh
I need to convert a string to a JavaScript-Date
Examples of the source-strings
Wed, 01 Apr 2015 07:30:42 CEST
Mon, 23 Mar 2015 08:00:15 CET
I tried parsing it with Date.parse and creating a new Date from it but it is not working, JS throws an "Invalid Date" error.
What is the best way to convert this string? Do I have to go all RegEx over it?
Thanks in advance!
Your timezones are not valid for Date.parse()
So you have to replace your timezones, something like this:
var dateString = "Wed, 01 Apr 2015 07:30:42 CEST";
dateString = dateString.replace("CET", "GMT+1");
dateString = dateString.replace("CEST", "GMT+2");
var date = Date.parse(dateString);
console.log(date);
// => 1427873322000
When I print out a date in javascript it adds GMT-0400 (EDT) to the end of it, is there a way I can cut this off? I'm using
date=Date()
document.write(date)
To get the date and time but I dont want the trailing GMT-0400 (EDT)
You should be able to just get the appropriate parts out of the date object:
var date = new Date();
[date.toDateString(), date.toLocaleTimeString()].join(' ');
// "Wed Oct 03 2012 12:13:56"
Use var date = new Date(); Date is a constructor and should be used with the new keyword.
You need to format it. The display, by default, is that full string.
You can use the Date API to build a string, or one of the various date formatting libraries, or even just manipulate it like a string (since it is):
var date = new Date();
document.write(date.split('-')[0]);
Also, you really shouldn't use document.write for a bunch of reasons, but I digress since your question was not about that.