Date conversion and manipulation in javascript with Arabic months - javascript

I want to change an arabic long-form date, i.e.
الخميس 26 فبراير 2015
(Thursday 26th February, 2015)
into a standard date using standard Javascript in order to manipulate the date (add a day), then display via Date.toLocaleDateString() to convert it back, making
2015 الخميس 27 فبراير
(Friday 27th February, 2015)
Is this a case of picking the date string apart, interpreting the arabic text as a month number and creating a new Date() given the numbers, or is there a prototype that converts an arabic date string into a javascript date? What language and optional parameters need to be used for Date.toLocaleDateString() to produce the same format of arabic date, as using 'ar' the numbers are returned in eastern arabic, as opposed to the required western numerals?

var months = ["يناير", "فبراير", "مارس", "إبريل", "مايو", "يونيو",
"يوليو", "أغسطس", "سبتمبر", "أكتوبر", "نوفمبر", "ديسمبر"];
var days =["اﻷحد","اﻷثنين","الثلاثاء","اﻷربعاء","الخميس","الجمعة","السبت"];
var date = new Date();
console.log("The current month is " + months[date.getMonth()]);
console.log("The current day is " + days[date.getDay()]);

Related

What is the time format used in Date constructor in nodejs?

I am running Nodejs in Lambda in Sydney region. I have below code:
> d=new Date('2020-12-14T13:20:44.733Z')
2020-12-14T13:20:44.733Z
> d.getDate()
15
It creates a Date instance with a date string 2020-12-14T13:20:44.733Z. But the date is translated to 15th of December not 14th. Does this mean the string 2020-12-14T13:20:44.733Z representing UTC time?
The process that is carried out is described in the specification here:
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Z is the UTC offset representation specified as "Z" (for UTC with no offset) or an offset of either "+" or "-" followed by a time expression HH:mm (indicating local time ahead of or behind UTC, respectively)
So the trailing Z means the time passed into the Date constructor is in UTC, but getDate will give you the local representation of whatever time is in the Date object.

What is simplified in ECMAScript 5 date format compared to ISO 8601

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
— https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
So what exactly is the difference between the two formats? What do I have to be careful about? I noticed that ISO 8601 states that the T can be substituted by a space. What else is "simplified"?
To be very specific: This question is about the standard. Browser behavior is interesting, but not the main focus of this question.
ISO 8601 defines lots of different formats for a date and time, like:
extended (same used by ECMA): YYYY-MM-DDTHH:mm:ss.sssZ
basic (same as above, but without - and : separators): YYYYMMDDTHHmmss.sssZ
And also the variations: just the date, just the time and with or without offset. If a date uses one of the above (basic or extended), the time must use the same.
And it also allows another formats for the date (not used by ECMA):
"incomplete" dates:
only the year: YYYY, or the expanded version with a + or - signal before the number
just year and month: YYYY-MM
just month and day: --MM-DD
week dates: YYYY-Www or YYYY-Www-D. Www is the week number (an uppercase W followed by 2 digits), but YYYY in this case is the ISO week-numbering year - quoting wikipedia:
The ISO week-numbering year starts at the first day (Monday) of week 01 and ends at the Sunday before the new ISO year (hence without overlap or gap). It consists of 52 or 53 full weeks. The first ISO week of a year may have up to three days that are actually in the Gregorian calendar year that is ending; if three, they are Friday, Saturday, and Sunday. Similarly, the last ISO week of a year may have up to three days that are actually in the Gregorian calendar year that is starting; if three, they are Monday, Tuesday and Wednesday. The Thursday of each ISO week is always in the Gregorian calendar year denoted by the ISO week-numbering year.
ordinal dates: YYYY-DDD (year and day of the year).
Example: 2017-02-01 is the same as 2017-032 (February 1st is the 32th day of the year).
The week and ordinal formats above can also be used with a time.
Example: 2017-02-01T10:00 and 2017-032T10:00 are both valid (and equivalent).
ECMA simplifies it by allowing only YYYY-MM-DDTHH:mm:ss.sssZ. It also allows extended years (with 6 digits and a signal), but keeping the same format for the other fields.
ISO 8601 also defines another concepts (Durations and Time Intervals). Although they're both related to dates and times, they're not the same thing: a duration is an "amount of time" (like "2 days, 4 hours and 5 minutes") and an interval is the "intervening time between two time points" (with start and end dates).
ISO 8601 is a standard for a whole range of time and date formats, including:
Date
Time of day
Coordinated universal time (UTC)
Local time with offset to UTC
Date and time
Time intervals
Recurring time intervals
The ECMA-262 specification (20.3.1.16 in the 2017 version) only implements the date and time.

Parse Date "01/01/2016 Fri"

I am trying to parse a date in JavaScript, but the particular format is giving me fits. I have exported data from my credit card company and the format of the date field is not compatible with Date.parse or moment().isValid().
E.g.
Date.parse("01/01/2016 Fri") // NaN
moment("01/01/2016 Fri") // false
I'm not sure if I should do something with a RegEx .test() or .matches() because this is being used for a CSV import utility where dates may be in different formats. I was surprised the utility functions above didn't work.
Look in the Moment docs to see how to parse a date in any format. The first argument is the date string, the second is the format string. Alphanumeric characters are ignored, so you don't need to worry about slashes vs. dashes.
moment("01/01/2016 Fri", "MM-DD-YYYY ddd)
Check out the Mozilla MDN on Date.parse():
The parse() method takes a date string (such as "Dec 25, 1995") and
returns the number of milliseconds since January 1, 1970, 00:00:00
UTC. This function is useful for setting date values based on string
values, for example in conjunction with the setTime() method and the
Date object.
Given a string representing a time, parse() returns the time value. It
accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g.
"Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US
time zone abbreviations, but for general use, use a time zone offset,
for example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes
east of the Greenwich meridian).
From this, it looks like your problem is that you're giving the date in the improper format:
It
accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g.
"Mon, 25 Dec 1995 13:30:00 GMT".
Check this out:
Invalid values in date strings not recognized as ISO format as defined by ECMA-262 may or may not result in NaN, depending on the browser and values provided, e.g.:
// Non-ISO string with invalid date values
new Date('23/25/2014');
TL;DR - you're passing the values in a format that is not recognized, which is why it's returning NaN.
Try this source for Regexes for dates: Regexlib.com. The site is a little out of date, but the info is great. It has tons of different Regexes for different date formats.

"Invalid date" error when converting a string to date

Am trying to convert a string to date
var strdate='2014-04-23+09:06:57.4830591330'
while trying to convert this string to date using below code
var followupDate = new Date(strdate);
console.log(followupDate)
am getting the error
Date {Invalid Date}
You just need a space rather than an addition sign, so you could just replace it:
var strdate='2014-04-23+09:06:57.4830591330';
var followupDate = new Date(strdate.replace("+"," "));
console.log(followupDate);
Will log something like: Wed Apr 23 2014 09:06:57 GMT+0100 (GMT Summer Time).
You should replace the + sign in your string for a space, between the year and the hours.
To explain this, let's see Date documentation :
dateString
String value representing a date. The string should be in a
format recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).
Now, from RFC 2822 (under 3.3. Date and Time Specification), it's explicitly described that the + is meant for timezones:
zone = (( "+" / "-" ) 4DIGIT) / obs-zone
Moreover, note that there's no room for milliseconds in this standard.
Your String Format is Wrong
It should be like this
var strdate = "2014-04-23 09:06:57.4830591330"
if you are getting your value dynamicaly then change it to this format by
var newstrdate = strdate.replace("+", " ");
and then try
date = new Date(newstrdate);

Possible return values for (new Date()).toDateString() in JavaScript?

The return value for (new Date()).toDateString() is "Mon Oct 08 2012". However I can't find ANY documentation anywhere for what the abbreviations for the rest of the days of the week and months are. Are they all just 3 character abbreviations? I'm trying to write a regex.
+1million points for someone who can find the documentation, or even the source code?
Three letter abbreviations with the first letter upper case.
Months: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
Days: Sun, Mon, Tue, Wed, Thu, Fri, Sat
However, you may want to look into Date.Parse() instead of using a regular expression to parse the date string, depending on what you're doing anyway.
EDIT: Beware that that Date.Parse() is fairly browser dependent. Check out Why does Date.parse give incorrect results?
It's just the standard English language abbreviation for days and months. Just the first 3 letters and the first one capitalized.
From the MDN:
Date instances refer to a specific point in time. Calling toString
will return the date formatted in a human readable form in American
English
It's not hard to find:
W3Schools: http://www.w3schools.com/jsref/jsref_todatestring.asp
Mozilla Developer Network: link
Microsoft Developer Network: link
As you can see, all of them converge, its the week day, the month name, both with 3 characters, day of month and full year.
The specification does not define the output of the string:
The contents of the String are implementation-dependent, but are intended to represent the "date" portion of the Date in the current time zone in a convenient, human-readable form.
This might change in the future, but for now, each browser/environment can produce a different output.

Categories