how to apply localized format on a date javascript [duplicate] - javascript

This question already has answers here:
Display date/time in user's locale format and time offset
(17 answers)
Closed 14 days ago.
I want to display a date on the page, and want to show it differently by the language in the browser setting.
for example: mm/dd/yyyy (in case of 'en') yyyy/mm/dd(in case of 'ko')
currently I use dayjs and it doesn't really help.
how to customize it?

Use toLocaleDateString with no options:
const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
console.log(event.toLocaleDateString('de-DE'));
console.log(event.toLocaleDateString('ar-EG'));
console.log(event.toLocaleDateString(undefined));
// 19.12.2012
// ١٩‏/١٢‏/٢٠١٢
// 12/19/2012

Related

Difference between javascript date.valueOf() and java date.getTime() [duplicate]

This question already has answers here:
Creating java date object from year,month,day
(6 answers)
java.util.Date and getYear()
(12 answers)
Closed 2 years ago.
Currently I am trying to convert a date calculation from javascript to java, since I want to use it for Android development. So in javascript I was using the method date.valueOf(), which converts the date to the milliseconds that passed since January 1, 1970. In java the method with the same funcionality is called date.getTime(). In the java and javascript documentation the description is exactly the same, but when I am inserting the same date, I am getting two completely different values.
For example:
Java getTime()
Date date = new Date(2011, 10, 1);
System.out.println(date.getTime()); //prints: 61278249600000
Javascript valueOf()
const date = new Date(2011, 10, 1);
console.log(date.valueOf()); //prints: 1320102000000
So my questions are:
Why is this happening? (or what did I wrong?)
How can I correct the code?
It would be awesome if somebody can help me.
The discrepancy isn't between Java's getTime and JavaScript's valueOf, it's the Java Date constructor. The year parameter is years since 1900. So the equivalent Java code would be:
Date date = new Date(110, 10, 1);
// ^−−− 2010 - 1900
This is one of the many reasons not to use the java.util.Date class.

Weird behaviour from JS Date() object [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I want to represent the first of a given month, say 1st of Septemeber 2020. When I do this in my console, I get the below output:
$ node
> new Date(2020, 8, 1)
2020-08-31T23:00:00.000Z
How come it returns 2020-08-31 instead of 2020-08-01? What am I doing wrong?
The output
2020-08-31T23:00:00.000Z
is ISO format, and the Z shows the date is in UTC. When you call the Date constructor, it is using your browser timezone by default, which I can assume is +01:00. Moreover, month indice in the constructor is zero-based.
If what you want is the beginning of the month in UTC, you can do:
d = new Date(Date.UTC(2020, 8, 01))
d.toISOString()
"2020-09-01T00:00:00.000Z"
try
new Date(2020, 7, 1)
because date object is count from 0-11 no 1-12

How to convert long date value to string format in JavaScript [duplicate]

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.

How the date can be formatted in JavaScript? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 6 years ago.
There is simpleDateFormat available in java,similarly is there any way to format the date in JavaScript
What is the best equivalent way to perform date formats in JavaScript.
I think In javascript we don't have straight forward functions to get dateformats, to get the exact format you need to call differnt functions on Date object(in javascript).
but in jqueryUI we have formatDate().
var d = new Date(2016, 0, 30) // 30 Jan 2016
alert( formatDate(d) ) // '30.01.16'

can't format javascript timestamp string toLocaleString [duplicate]

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

Categories