I have a date string which looks like this: 2013-04-06T14:15:00
I'm looking for functions similar to toLocaleDateString()
(documentation). However, those functions don't take a String parameter; you need to create a Date object first. I'm trying to avoid timezones altogether, so does anyone know of a function (standard or from a plugin) which can format a datestring using a specific locale's rules (1/17/2013 vs 17/1/2013 etc.) using only my datestring?
I'm currently using jQuery, and this plugin for formatting dates: jQuery.dateFormat
Pass your date string to the Date constructor. It parses most legitimate formats.
new Date("1/17/2013")
new Date("2013-04-06T14:15:00")
Related
I have this method below that takes in a Javascript date and then has to use Moment js to do its timezone conversion.
QUESTION - Is there a way to accomplish this without moment, just using a JS Date?
// ex. (someDate, 'America/Los_Angeles', 'MMM DD # h:mm A z')
transform(dateUtc: Date, timeZone: string, momentFormat: string): string {
const tempDateUtc = moment(dateUtc).utc(true);
tempDateUtc.tz(timeZone);
return tempDateUtc.format(momentFormat);
}
To apply specific date formatting, rather than a locale based format, then no, not really, but it is possible to get the localized date parts to create a custom format.
As #RobG mentioned, you can use the Intl.DateTimeFormat to create a formatter. You build your options, including your timezone, and then use it's formatToParts(date) method to get the parts you need to construct your output.
Of course, this only works well if your Date is constructed using an epoch or UTC value.
I'm making an api call which retrieves a set of objects. One of the objects returns a date and time together like this:
createdAt: "2020-11-04 09:48:32"
This is where I display the date and time:
<template v-for="item in collectedTrash">
<BeforeAndAfter v-if="item.isPresented === 1"
:key="item.username"
:avatarUrl="require('#/assets/img/images/img_stats_km#2x.png')"
:imageBefore="getImageUrl(item.imageUrlBefore)"
:imageAfter="getImageUrl(item.imageUrlBefore)"
:username="item.username"
:date="item.createdAt"/> This is where I get the date
</template>
Is there anyway that I can retrieve the date only, rather than the date and time?
I am assuming you don't have access to the api and therefore have to process the date in your vue application.
Do you need the date as a date Object or is a string fine?
If a string representation is enough, you could use a library such as Date fns and use the format function:
...
:date="format(new Date(item.createdAt), 'yyyy-MM-dd')"
Another option might be to only use the first 10 characters of the string you received as the date: date="item.createdAt.substring(0,9)"
Did you try something like: :date="item.createdAt.substr(0, 10)"
In my experience the easiest way to format datetimes in JS it to use the Moment.js library (https://momentjs.com/). You could reformat the datetime string before passing it to your child component.
Or, if you don't want to rely on a third-party library and you know that the datetime string will always be passed in that format, I suppose you could do item.createdAt.slice(0, 10).
Moment.js can be very helpful with dates and times
Moment Docs
moment("String").format('L'); // 04/11/2020
I have a javascript Date object as below
var d = new Date();
console.log(d); //2019-11-28T04:27:43.268Z
I want this date to be formatted according to the user preference
Eg:
d-m-y : 28-11-2019
m-y-d : 11-2019-28
y-d-m : 2019-28-11
...... etc
Is there any way to do this in javascript without manually format the date(using regex or something else)?
Note: In Java I can achieve this by using DateFormat class format(Date date) method from the JDK
You can use Day.js API for easy conversion functions on date parameters.
Day.js
formatting DateTime
Day.js objects are formatted with the format() function.
Day.js github Link
I am currently having some issues converting a string dateTime object in JavaScript
I am assuming it is because my string cannot me used properly in a new Date() but I'm not sure that is the problem.
My Input: "2011-09-29 14:58:12"
My code:
var date = "2011-09-29 14:58:12";
var added = new Date(date);
var year = added.getYear();
However, my year var contains NaN. Same with getDay() or getMonth(). What is the problem?
ps: I'm getting the date in it's format from a SQLite database. And I'm using Titanium Mobile, so javascript and SQLite are the only things involved
You're relying on the Date constructor parsing an unsupported format. Until recently, there was no standard string format supported by the Date constructor. As of ECMAScript5, there is one (YYYY-MM-DDTHH:MM:SS, note the T rather than space), but it's only been specified for just under two years and naturally doesn't work in older browsers.
For the time being, your best bet is to parse it yourself (you can find code in this question and its answers), or use something like DateJS, MomentJS, date-fns, etc. to parse it for you.
The Date constructor will not parse a string for you. You'll need to use Date.parse to do that. Interestingly enough, Date.parse doesn't actually return a Date. Instead it returns a unix timestamp. You can then pass the unix timestamp into the Date constructor to get what you're looking for.
var d = new Date(Date.parse("2011-09-29 14:58:12"));
It appears I can't use the javascript Date object as it inherintly defaults to US dates when you initialise with a datestring. There is no way of passing any culture information to the date object
I.e. No matter what the clients locale settings are
var d = new Date("08/10/2009") will always create a date object representing the 10th August 2009 rather than the 8th October 2009 if the clients locale was the UK.
So given that my requirement is to be able to add/subtract days/months/years easily is there a clever way of doing this easily without the Date object
All i need to do is add a day to a date (or a string representation of a date). so if my code detects the locale setttings are in the US, when it sees a string like "10/08/2009" it whacks it up to "10/09/2009" but if it had detected it was in the UK it would have know it a uk string representation of a date and whacked it up to "09/10/2009"
For date manipulation and localization on JavaScript I always recommend the DateJS library.
This library abstracts the use of Date objects, has a very good localization options, powerful date parsing and formatting, and it also has a very nice fluent API.
If you know you are getting input formatted dd/mm/yyyy you can easily assemble the correct date.
function britDay(D){
D= D.match(/\d+/g);
return new Date(+D[2], D[1]-1, +D[0]);
}
toLocaleDateString will return the date in the format expected by the user.
Relying on the user input that obeys particular formatting rules is optimistic-
which is why most sites use separate, labeled inputs or select fields for the month, date and year.
You probably know that it's easy to add one day to a date, just add 86,400 * 1000 milliseconds to the date. It sounds like displaying in your locale is the issue; does Date.toLocaleString() not do the right thing for you?
dojo.date.locale.parse will be able to parse a formatted string according the locale of your choice. It has a table of cultural data based off unicode.org/cldr. See this article for more information.