Dart - Extract Date (MM/DD/YYYY) from a String - javascript

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"

Related

Momentjs is not converting certain Spanish months to UTC

I am working on a React Native application which is available on two languages (English & Spanish). Getting date in English is fine but for Spanish, Momentjs is not able to convert it to moment object for certain months(in spanish) like Abril, It throws Invalid date as output, while other month like Septiembre is working fine and it is getting converted to moment object.
const aprilDateES = moment("09 Abril, 2021") // Invalid date
const septeDateEs = moment("09 Septiembre, 2021") // Thu Sep 09 2021 00:00:00 GMT+0530
It is also throwing warning for Abril month -
My goal is to convert all Spanish date/month to moment object so that it can be converted to other format like DD/MM/YYYY and can also be validated using momentJS.
Here's the link to playground - https://replit.com/#DevAk1/MomentJSSpanishDate#src/App.jsx
Change import to -
import moment from 'moment/min/moment-with-locales'
Then use this -
const date = moment('09 Abril, 2021', 'DD MMMM, YYYY', 'es').locale('en')
// Fri Apr 09 2021 00:00:00 GMT+0530
Note - This is specific to format and language. Please change the source/target language and the date format according to the requirement.

Date format in javascript and SQL

I have a string "03/31/2017". I need to pass it as a Date to the SQL database. I tried to use new Date("03/31/2017") but it returns Thu Mar 30 2017 00:00:00 GMT-0400 (Eastern Daylight Time) Is there anyway I can keep the original date format as a data object without using momentum or anyother library. I can't use any library but jQuery.
The correct date format for Javascript is: YYYY-MM-DD
var dt = new Date('2017-03-27');
To be safer, do NOT use a string to specify the date, instead supply the parameters:
var dt = new Date(2017, 3 - 1, 27); // months are zero based

Javascript date object automatically add one day when creating from date string

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.

javascript date format conversion (full text date to unix time stamp)

I have a date in the following format
Fri Mar 16 2012 05:53:18 GMT 0200 (GTB Standard Time)
And I want to convert it into a unix timestamp.
Until now I manually split the string by spaces and then I am giving it as an input to a Date object, in order to get milliseconds in a latter step.
Is there any easiest way?
(I am trying to avoid jQuery plug-ins and do it using vanila javascript)
Yes, the easiest way would be to pass the string to Date object and then call the getTime method:
var myDate = new Date('Fri Mar 16 2012 05:53:18 GMT+0200 (GTB Standard Time)');
console.log( myDate.getTime() ); //1331869998000
No need to split your string by spaces.

How to convert a JSON string in JS date object?

{"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?

Categories

Resources