How to get simple date (“Month Number”)? [duplicate] - javascript

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);

Related

how to display date like below image? [duplicate]

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

Timestamp to date format and reverse [duplicate]

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 1 year ago.
Hello Guys!
I just wrote some code with JavaScript and Firebase, I obtained the date from Firebase in milliseconds and converted it to date format with this code.
const dateObject = new Date(milliseconds)
const humanDateFormat = dateObject.toLocaleString("en-US", {day: "numeric"}) + '/' + dateObject.toLocaleString("en-US", {month: "numeric"}) + '/' + dateObject.toLocaleString("en-US", {year: "numeric"});
Now I cant find the way back with this kind of date format I used. I want to edit the product but when I try to convert this date I can´t find a useful method.
Maybe should I use LocalStorage to storage the date in milliseconds then search for it and use it?
Thanks anticipated!
Regards, Gaspar.

JS formatting date ymd to yyyy-mm-dd [duplicate]

This question already has answers here:
Moment.js - How to convert date string into date?
(3 answers)
Closed 3 years ago.
I am getting my date from PHP in format ymd. I am using Moment.js to convert it to format I need, but it's not working properly.
So for example today's date is comes like 190528 but after conversion to YYYY-MM-DD it becomes 190528-1-1 instead of 2019-05-28.
Is it the first format of the date which causes the problem, or there is a way to overcome this and convert it the way I need?
Parse Date first by passing format as 'YYMMDD'
moment("190528", "YYMMDD").format("YYYY-MM-DD");
This will give proper output.
You do not need to load moment for this.
You can break the string into parts of years, month and date. Format the year and return the joined string...
function formatDate(date) {
var _d = date.match(/.{1,2}/g);
_d[0] = "20" + _d[0];
return _d.join("-");
}
var d = "190528";
console.log(formatDate(d));
You need to tell moment what format your date string is in:
console.log(moment("190528", "YYMMDD"))
<script src="https://unpkg.com/moment#2.24.0/moment.js"></script>
You can pass the format of current date as a second param of moment.
console.log(moment("190528", "YYMMDD").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here the docs.

How can I convert a date string in to a specific date format in javascript [duplicate]

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 3 years ago.
Convert date string in to a specific date format in JavaScript.
I am trying to get this output. Here I will give date format dynamically. It should converted to date object. Here dateString is a string.
var dateString = '03/04/2019';
var format = 'dd/mm/yy';
var dateObject = foramtDate(dateString , foramt)
This is what I'm using:
// convert string with format 'dd/mm/yy' to Date object
function stringToDate(dateStr) {
const [day, month, year] = dateStr.split("/");
return new Date(year, month - 1, day);
};
It's only for one specific format.
UPDATE:
The above script will not work in IE. It uses array destruction which is not supported by the browser. More info here

How to change the format of a date in JavaScript? [duplicate]

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.

Categories