In Moment.js, why doesn't subtract by 'week' work? - javascript

I'm using Moment.js and following line of code doesn't seem to actually do anything:
moment().subtract('week', 1)
In the documentation, it shows an example of subtract being used with 'days' but it doesn't specify that it's the only string you can use. In most of the other functions, it's an option where you can use 'days', 'weeks', 'months', so I'm not sure if I'm doing something wrong or 'weeks' just isn't supported:
Moment.js Subtract Documentation
Here's the example of subtracting days:
moment().subtract('days', 7);
It's also what I ended up using instead of 'weeks' but I'm still curious why 'weeks' aren't supported.

You have it backwards from the Moment API.
moment().subtract(1, 'week');

This is a bit old question but it appears that even back then it was relevant.
Definetely nowadays is even more relevant to consider parse.com moment.js version (1.7.2) to the current moment.js version (2.8.4) and the only API doc that parse.com makes reference to
Check an answer to this in a previous post in Trouble using the Moment module
I would suggest to work always with latest moment.js so you can properly work with the provided documentation (during my search I was not able to find 1.7.2 documentation.. beside you will miss many great features working with the version provided in parse.com.
Add the new version as indicated in the same post in the accepted answer.

Related

JS - standalone user-input date interpreter

I've spent literal hours searching the web for a JS library or a Node module that does just one thing: interpret user-input dates of all kinds (just english is fine) and converts them to a standard date.
For example, it should be able to recognize and convert the following into actual dates:
tomorrow
in a year
last week
in 8 hours
in 4 months
8 8 90
65
Sugar.js does this, however I don't want all its other features, and it extends native objects. Moment.js deprecated its ability to do this as they consider it too opinionated - fine.
One would think someone made a library for this. Does anyone know of any?
In cases like this where a library provides a functionality that I need but I do not need the entire library, I will just strip out the portion of the library that I need.
Sugar.js actually provided a customize option, so this is even easier to do.
http://sugarjs.com/customize
Additionally if you use bower or npm for your package management, there is already a package that just has the Date functions from Sugar.js:
https://github.com/fiznool/sugar-date

difference between moment js functions and javascript date functions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Actually i am thinking about to use moment.js in my new project.because my new project is a employee working sheet application.
I read the moment.js documents.
We can get the current date by using the below code in moment.js
moment().valueOf();
We can get the current date by using javascript
new Date()
The both are giving same result. with same speed (so no performance issue )
also formatting, get methods and set methods are already has javascript. then why i go to moment.js?
Please give me some explanations about the difference's. And let me know which one is best for my new project.
Moment.js
A lightweight JavaScript date library for parsing, validating, manipulating, and formatting dates.
Reason why we use Moment.js - Many web applications today, especially social ones, deal with the concept of time. They are usually employed to sort events or posts, or to mark the moment in which something happens. You can think of your Twitter timeline or your GitHub news feed. JavaScript has a Date object that you can use to manipulate dates, but it often doesn’t have all you need in your web applications
The Moment.js library not only parses dates from strings, but it may also be used to validate, manipulate, and format dates. It supports internationalization, which is so important with dates, as well as human-friendly formatting like "Last Friday at 9:48".
These are all good things for sure, but today's order of the day is date parsing, so let's get into how that works.
Moment.js creates a wrapper for the Date object rather than extend it. To reference the wrapper object, simply call the moment() getter function. The Moment prototype is exposed through the moment.fn property, so you can add your own functions to it if you are so inclined.
Courtesy of
Managing Dates and Times Using Moment.js
A Roundup of Popular JavaScript Date Parsing Libraries: Moment.js
Moment.js is convenient when you want to manipulate Dates.
An Example for moment is given below
moment()
.add(7, 'days')
.subtract(1, 'months')
.year(2009)
.hours(0)
.minutes(0)
.seconds(0);
moment().endOf('day').fromNow();
But if formatting, getting and setting is all you need, you might not need a library for that.
Obviously, I would suggest to go with moment.js. It is one of the most popular Date parsing library.
moment.js has a various inbuilt function's available which is easy to use.
It will save your development time.
Manipulating and parsing dates is very easy.
It comes down to what you're trying to do.
MomentJS provides an API that pretty-much wraps the native Date object, which is why you call .valueOf. new Date is simply giving you the native Date objects (and to manipulate it using moment, you'd need to pass it to its constructor).
I would say general rule of thumb:
If you're creating a date for use on the site or as some kind of UI element, use Moment.
If you're storing a date back to a service layer, use Date.
If you're working in a method that's manipulating the date, use Moment; If that then needs to be passed to another service layer, call .valueOf and save it off.
Moment is great for the UX, but isn't a "standard" (and therefore wouldn't be ideal for serialization/storage).
Moment.js has a good support for adding, subtracting, advanced formatting, finding differences between two days. Also it has internationalization support so you can convert to any language very easily.
just like any library comes with a purpose to aid with something and make it easy to work. momemt.js makes working with dates PAINLESS.
working with dates considering the timezone and formats factors etc is very difficult, but moment.js makes it a breeze to work with dates.
read more here

RobinHerbots jquery.inputmask date range

I am using RobinHerbots/jquery.inputmask library.
I need to make sure the date inputted in great than current date.
There is code for yearrange but I dont see anything for basic 'daterange'.
Using RobinHerbots/jquery.inputmask library, how can I make sure the date entered is greater than current/todays date new Date()?
There is an open issue about your question in the github of RobinHerbots/jquery.inputmask:
RobinHerbots answers that this feature will be implemented in 3.4 and that for now you can use the postValidation callback.
In the current date alias this is not implemented. However, you can use the postValidation callback to validate if the input is after the current date.
It would be a nice feature, and I will add it to the date alias in milestone 3.4

Receiving incorrect datetimes when using milliseconds for x-axis?

Here is a link to the jsfiddle. For some reason my datetimes are showing up incorrectly as you can see below.
I have confirmed that the milliseconds were correct so I'm led to believe that it's a bug with the library however I've seen examples using Date.UTC that work correctly but I can't seem to find any examples in their documentation that uses milliseconds instead.
Millisecond values were: 1326113281133,1326113401128, 1326113521102, 1326113641108,1326113761129,1326113881139,1326114001156,1326114241198.
Any help would be much appreciated in helping to find out the cause of this issue.
its not a bug in the library, the timestamps are shifted to Universal Time in the chart.
use the following code before your chart variable declaration
Highcharts.setOptions({
global: {
useUTC: false
}
});
here is the reference to use this option
http://www.highcharts.com/ref/#global--useUTC

how to convert Hijari date into Gregorian date in javascript?

I am using intalio editor in that I want to convert the Hijri date into a Gregorian date or vice-versa. we write code in javascript using jsx3 so please help me.
For Gregorian->Hijiri, see here
Bidirectional C# version here that could be converted to Javascript
EDIT: An excellent page and convertors can be found here. That page says:
All calculations are done in
JavaScript executed in your own
browser; complete source code is
embedded in or linked to this page,
and you're free to download these
files to your own computer and use
them even when not connected to the
Internet.
which suggests to me you can use the code, but you probably want to check with the page author
It helps to know that muslims refer to the Gregorian calendar as Masihi. Googling for "hijrah to masihi converter" turns up this page as the second hit: http://www.islamicity.com/PrayerTimes/defaultHijriConv.asp
The javascript is code contains two functions GregToIsl and IslToGreg that does what you want.
There is a copyright notice on that page so you shouldn't simply copy-paste the code. But the functions are short enough for you to extract the relevant maths out of.
The maths/algorithm itself is almost a thousand years old so it shouldn't have any legal restriction.
Try dojox.date.islamic
In Javascript the correct way for such conversion is to use Intl object (read more) as following:
a = new Date();
localeFormat= 'ar-SA-islamic-umalqura';
Intl.DateTimeFormat(localeFormat).format(a)

Categories

Resources