Getting %FD in resulting string when using Date.toLocaleString in javascript - javascript

I was looking for a way to get the month name and day-of-week name from a Date object in javascript and came across this post: Get month name from Date where David Storey pointed to the date.toLocaleString() method. At first it looked like this was working great. I could do something like this to get the month name without having to define an array:
var someDate = new Date(2018, 8, 28);
var locale = "en-us"
var month = someDate.toLocaleString(locale, { month: "short" }).toLowerCase();
that would give me a result of aug in the month variable. But I noticed a problem with this. Ultimately I was dropping the month and day-of-week values into hidden form fields that would get posted with the form. When the form posted I wasn't getting the expected values. For example, looking at the posted value for the month field, instead of aug I was getting %FDaug, and the same was true for the day-of-week value. But if I checked the values in the debugger prior to posting, they looked correct. I don't know where the %FD was coming from but it means I can't use this method unless there is some way to trim that off first. I looked at documentation for the function here but didn't see anything there related to this issue.
For now I'm back to using an array of month and day names and doing a lookup to get those. Anyone know why that extra ascii character is introduced when using Date.toLocaleString?
Here are screenshots from the browser debugger, I've tried this in IE, Edge, and Chrome. Chrome appears to be ok, but IE and Edge have this behavior. I haven't tried Firefox
the values before posting...
the posted values...

Related

IF THEN using a Timestamp

Do IF statements work using timestamps? I'm using a Google Sheet from a Google Form and would like to use the Timestamp as a matching identifier.
Code:
if (time == homeTime) {
s.getRange(7, 1).setValue("Yay!");
Logger.log shows both variables are indeed the same value (Tue Jun 26 08:47:52 GMT-04:00 2018).
It works if I choose a value other than the timestamp, such as an email address.
I cannot use < or > because I'm using this in a for loop. As a temporary fix, I changed the format of the column to Plain Text using #, but time - homeTime == 0 is the answer I needed!
I'm trying to figure out how to mark my answer correctly so I apologize if I mess it up.

Java Script return Invalid Date for Date with time set for "11:59:59 PM"

This is driving me crazy for an hour.
Here is a snap shot of my Chrome console.
You can see how the default js Date() function is behaving so inconsistently with different dateTime string provided to it.
Anyone knows anything? How should I deal with it?
Thanks
Some browser use the "dd/mm/yyyy" format, other "mm/dd/yyyy" and so on, either way, for you to not get invalid date you need to know which format the date class/function/method will use so you can pass the date string in that order.
Obviously the browser you tested on use "mm/dd/yyyy" and therefore your first date is invalid as there exist no a month with number "30" as in "30/09/2015".
Of course it would be clever if browser could guess and in this case it would be easy but this, "10/12/2015", it wouldn't, as both the "mm/dd/yyyy" and "dd/mm/yyyy" will have a corresponding real date, "10 of Dec" and "12 of Oct", and we can't let the browser decide which one is the one we mean, as both will pass as valid.
Check this question for a deep dive into the issue and as well a couple of ways how to solve it
- Why does Date.parse give incorrect results?
Your format is wrong. There is no 30th month.
Try this,
new Date("09/30/2015 11:59:59 PM")

How do I format a Javascript date like 1950-12-30T18:25:43.511Z

I have an API where I the date need to be formatted like this
1950-12-30T18:25:43.511Z
This format looks somewhat unfamiliar to me, and I'm wondering how I can take a format like this
1950-12-30
and turn it into something like the former. FYI I'm using javascript (express)
You are trying to put the date into ISO format and the native Date object will do that for you, fairly simply, with the .toISOString() method:
var newDate = new Date("0977-03-28");
console.log(newDate.toISOString());
The result of that is: 0977-03-28T00:00:00.000Z (look familiar? :D )
One issue that you will have (if it is an issue), is that, because you only have the date value, and not a time value, the last part of the string will always be T00:00:00.000Z (which is the "time" section of the Date). You'll see that if you use today's date, using var newDate = new Date(); (which captures this instant), the time will be filled in: 2015-02-19T16:50:18.078Z (at the time of testing)
For more information, see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
(Note: in IE, this only works in v9 or later, though, that link also has a polyfill for older IE versions)

Moment.js 2 different date strings when i parse using moment gives same value

I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.

What is needed for a valid Javascript Date object?

I've been banging my head over this one all day. No matter how I initialize a Javascript Date I cannot seem to get a valid Date object... I'm assuming the Date is invalid and not working properly by inspecting it with Chrome's debugger, which has the value '__proto__: Invalid Date'.
I've tried all of the following:
var d = new Date();
var d = new Date('2012-10-08');
var d = new Date('2012-10-08 00:00:00');
var d = new Date(Date('2012-10-08'));
var d = new Date(Date.parse('2012-10-08'));
var d = new Date(2012,10,08);
var d = new Date("October 13, 1975 11:13:00");
Along with countless other attempts.
This is presenting a problem in iOS where I'm trying to get values from these Date objects but every function just returns NaN. I'd prefer to avoid having to use external libraries or have to convert YYYY-MM-DD format into any other format since I'm trying to get this to work with an HTML5 input type="date" with minimal code for a mobile site.
Essentially this boils down to: What are the parameters that make a Date object valid?!
Do not trust the Date object to parse strings, you must do it manually. Given the format 2012-10-08,
function stringToDate(s) {
s = s.match(/\d+/g);
if (s) {
return new Date(s[0], --s[1], s[2]);
}
}
You may want to do some validation of the input string and the resulting date object, the above just shows the conversion.
Edit
BTW, the only string format that seems to be parsed consistently in all browsers is the US-specific month/date/year format. There is no specification to support its use, nor is there any reason to believe browsers will continue to support it other than pragmatism and "legacy" reasons.
For the vast majority of regions, '2/3/2012' is interpreted as 2 March, so getting 3 February might be unexpected.
Once older versions of IE are no longer in use (probably a few years yet), it should be safe to use the ISO8601 extended format per ECMA-262. However, even browsers that support it are inconsitent. e.g given:
new Date('2011-02-29');
Firefox 15 returns 'Invalid Date', IE 9 and Chrome 22 return a date object for 1 March, 2011.
There are three ways of calling the method:
The number of milliseconds from the epoch:
new Date(milliseconds)
Any IETF-compliant RFC 2822 timestamp:
new Date("November 2, 1988 10:00:00");
Individual args:
new Date(year, month, day [, hour, minute, second, millisecond])
new Date(1988,11,02,10,0,0);
If your main concern is about parsing, have a look at Moment.js which is clearly superior to parsing things yourself. (IMHO)
Turns out jQuery doesn't bind the .change() event to input type="date" properly in iOS. Changed the event to .blur() and everything seems to work now. However, it still seems it is impossible to create a valid date object in Chrome... an issue for another day.
Thanks for the help everyone!

Categories

Resources