JS native format date method - javascript

I've got trivial question for which I can't find simple answer - how can I format Date object in order to get it in format I need? For example, "20.01.2014". I know about moment.js library but I must do it using only native JS. Please, give me answer. Thanks!

You use the getHours, getMinutes, and getSeconds (or getUTCHours, getUTCMinutes, and getUTCSeconds) methods on your Date instance, which give you numbers, and then do the formatting with string manipulation.

Related

JavaScript : Convert TimeZone Offset to TimeZone String (Eg: "-5.00" to "CST")

I am getting a timezone offset as "-5.00" from a service response and I need to format it in the UI as CST. How do I convert this in JavaScript? I searched and am not sure there is any direct method in JavaScript.
I would look at using Moment. It provides a full timezone library. You may be able to make use of it still depending on your requirements and situation.
http://momentjs.com/timezone/

Using moment.js, how to display the current date format for the user?

Given a text field, I want to have a suitable placeholder. A typical placeholder will be something like: "mm/dd/yyyy".
However, I would like to use locale-aware dates using moment.js.
This means that I will be specifying "l" as the moment.js date format, howe do I determine the date format that moment.js will be using in this case?
The user will not understand what "l" means, so using this value in the placeholder text makes very little sense.
Specifically, I am hoping to be able to access something like moment's internal "defaultLongDateFormat". (Though that is merely a default - moment.js probably updates it or has some other mapping at runtime for locale-aware date formats - I would like to access that mapping.)
EDIT:
There are multiple downvotes (who aren't explaining why they're downvoting it).
I think this is because they arent' understanding the question, so here are some examples:
I want a function such that:
getFormat("l") -> "mm/dd/yyyy", or equivalent for the US locales.
getFormat("l") -> "dd/mm/yyyy", or equivalent, for the AU locales.
I do not want to format a given date, or to parse a given date - I merely want to determine it's user-friendly format given an arbitruary moment.js format, specifically, for 'l'.
I don't think it's exposed nicely, but if the browser has its language configured correctly you can do something like this:
var lang = navigator.languages ? navigator.languages : navigator.language;
moment().locale(lang).localeData()._longDateFormat['L']
Languages behave slightly differently depending on which browser you're using, so don't know how reliable this is.
Follow up to Adam R's answer:
Seems to have been exposed by now:
localeData.longDateFormat(dateFormat);
returns the full format of abbreviated date-time formats LT, L, LL and so on
(source: http://momentjs.com/docs/)
Get the currently used locale data by moment.localeData()

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

How to get json date from javascript date object?

Hi is there any native way how to get json typed date from Date object? I mean I need to get date for today in this format: 2013-04-05... (I know I can use strings but I want something more native...)
In ES5 compliant browsers, there's Date.prototype.toJSON and Date.prototype.toISOString which does exactly the same thing.
If feature detection shows toISOString is missing, you can add it to Date.prototype using the code suggested at MDN.

JavaScript Date Validation RegEx OR JS?

I'm using a date picker built in to my device and it returns the date in format YYYY/MM/DD
and returns the time in format HH:MM 24 hours.
The overall returned string is YYYY/MM/DDT/HH/MM
T being the separator.
What is the best way to validate this so that it matches the format YYYY/MM/DD/T/HH/MM
Regex ? Does JavaScript have validation for date/time built it even if i have to split at T so i have the date and time in separate variables?
Whats the best way/algorithm to do this aswell preformance wise?
If i split my varible in to array like...
["YYYY/MM/DD","HH:MM"]
Is there a regex or js function that validates this best? Preformance is key.
Thanks
I wouldn't be worried about performance, you're not going to be doing this hundreds of thousands of times in a tight loop, presumably.
A regex is a good way to do it, and will be easy to write; if you create the regex once and reuse it, it should perform well too.
Assuming your example of the format is a typo and you meant YYYY/MM/DDTHH:MM, a modern JavaScript engine should also support using that string as the argument to new Date(...) provided you change the / to - (which you can readily do via String#replace). That's because it fits the pared-down ISO-8601 format defined as part of ES5. But older engines (IE8 and earlier, for instance) may not support it, you'd have to test on the browsers you intend to support. But if it's really YYYY/MM/DDT/HH/MM, ignore this paragraph.

Categories

Resources