This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 6 months ago.
I'm trying to format a Date String in React Native.
ex: 2nd NOV.
My problem is I have to show as it mention in the image.
enter image description here
You could use MomentJS https://momentjs.com/ - it is a nice easy Library that lets you manipulate Dates and also Format it the way you want it. Check the "Display" Section in the Docs and there the "format" Function.
I guess in your case it would look something like this:
Moment(yourDate).format("DO MMM");
A js alternative :
new Date().toLocaleDateString('en', { day: 'numeric', month: 'short' })
it would display the day and month according to the locale you choose
Related
This question already has an answer here:
.diff is not a function on moments.js
(1 answer)
Closed 4 months ago.
I have a date "22-Mar" (YY-MMM) and I want to format it in "01.03.2022" and after that transform it into a date object without using "new Date()" method.
I heard that I can do this with MomentJS but I think I write something wrong.
I tried to format this date like that but this doesn't work.
moment(date, 'YY-MMM').format('MM/01/YYYY').toDate()
My error message:
TypeError: moment(...).format(...).toDate is not a function
How should I resolve the problem?
format() returns a string, where as the toDate() is only available on the MomentJS object.
So remove the format() if you want to convert it to a Date
If you use the 'format' to set the day to 1, you can use date() for that: .date(1)
Also, your custom format was invalid, I've changed it to DD-MMM to the date is proper parsed
const input = '22-Mar';
const mom = moment(input, 'DD-MMM').toDate();
console.log(mom);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 1 year ago.
Im trying to get the date, but im wanting to to return ex ”Oct 29”. Ive tryed using split, but it changes everyday. Heres what I have so far.
Let date = Date().split(“ 2021”)[0];
This returns day<string> month day<int>
You can use the toLocaleString function to achieve what you want. This code below will generate the output you wanted ("Oct 29").
let date = new Date().toLocaleString('en-US', { month: 'short', day: 'numeric' });
You can format it there are various inbuilt methods or do this:
Let date = Date().toString().substring(4,10);
This question already has answers here:
Converting Unix timestamp to Date Time String [duplicate]
(2 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
I want to know how to convert a long date like this 1542814586896 into a String format like this 2019/02/05
You can use Date class for setting time in integer format and getting any values like day, month, year
let date = new Date(1542814586896);
console.log(date.getDay(), date.getMonth(), date.getFullYear())
You can use
new Date(1542814586896).toLocaleDateString(`ja-JP`);
//-> "2018/11/21"
.toLocaleDateString() formats time into a format of a specific region. In the example above, time's formatted into Japanese format (just because it seems like in Japan they use exactly the format you need).
What's cool about this method is that you may just pass no argument to toLocaleDateString & it will then just automatically pick the format that the final user prefers (or more precisely, the format that is set in user's OS).
For example in my browser:
new Date(1542814586896).toLocaleDateString();
//-> "21/11/2018"
However, if I had Egyptian Arabic set as main language of my operating system, the result should be like:
new Date(1542814586896).toLocaleDateString();
//-> "٢١/١١/٢٠١٨"
You may find more information about different locales & corresponding formats here.
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to display a Date object in a specific format using JavaScript?
(7 answers)
Format a date string in javascript
(7 answers)
Closed 4 years ago.
I have a date formatted as 2018-06-13T13:28:14+0000, and I want to change it into this format 13/6/2018 1:28:14 PM.
Any suggestions will be helpful.
Thanks, in advance :)
Your date 2018-06-13T13:28:14+0000 is in UTC format.
One of the simplest options is to use momentjs to get desired date time format.
moment(yourDate).utc().format("DD/MM/YYYY hh:mm:ss a")
Below is code:
let date = moment("2018-06-13T13:28:14+0000").utc().format("DD/MM/YYYY hh:mm:ss a")
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
the toLocaleString() function with the en-USlocale is close to what you are searching for. Would this be enough for you ? It will give you back something like :
today.toLocaleString("en-US");
// will give you => 6/15/2018, 9:03:58 AM
If you really need no comma, then use the toLocaleString() and then remove the comma like :
today.toLocaleString("en-US").replace(',','');
see : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Date/toLocaleString
To format a Date in javascript you can use Date.toLocaleDateString() method, it will give you many options to format dates.
This is a sample code snippet, you could use:
var d = new Date();
var options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true
};
console.log(d.toLocaleDateString('fr-FR', options));
Note:
Of course , you can play with options to get more possible results, you can check that in the Docs as well.
Otherwise, as suggested, you can use a JS library like Moment.js or date-format, they have better enhanced features.
This question already has answers here:
Problem with date formats in JavaScript with different browsers
(4 answers)
Closed 7 years ago.
new Date().toLocaleString() --> "24/09/2015 10:14:00 PM"
new Date("2015-09-24 09:38:32.639").toLocaleString() --> "Invalid Date"
How can I format a date object from a timestamp in string format?
SOLUTION: At the end I got it fixed changing my date type in the server from DateTime to Instant, js will atomatically add zone offset automatically from a timestamp and will format the dates in the right way.
NOTE: I know this question is duplicated, however the solution proposed is different and may help other users to get a different approach to their code.
var myDate = "2015-09-24 09:38:32.639";
new Date(myDate.replace(/-/g,"/")).toLocaleString()
Now it's working fine