I believe the answer presented by Caique Romero I would like to leave this reply in order to enrich the topic.
It seems to me that the author seeks to internationalize his software/system and convert a locally formatted user input (en-BR in this case) to "en-US" however, the format "suggested" by the author YYYY-MM-DD is not the usual format of the English (American) language that uses by default MM/DD/YYYY.
The following example represents the "correct" output if the intention is internationalization:
let dateObj = new Date('02/03/2018 00:00:00')
let dateString = dateObj.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute:'2-digit',
second:'2-digit'
}).replace(/\//g, '-')
console.log(dateString)
A simple replace()
replaced "bars" if this is really important.
Note that the format of hours in "en-BR" uses 24 hours while the format "en-US" only 12 so the output will always have "AM" or "PM" to indicate which period of the day is being referenced.
The formatting of the English language also differs as to Americans and British, British use the format DD/MM/YYYY as in this example:
let dateObj = new Date('02/03/2018 00:00:00')
let dateString = dateObj.toLocaleString('en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute:'2-digit',
second:'2-digit'
}).replace(/\//g, '-')
console.log(dateString)
British use 24-hour format so there is no "AM/PM" in the output, looks like our "en-br" the only difference is in a comma after the date.
The following example uses locally obtained Unixtimestamp (en-BR) and uses it to convert to standard "en-US" and "en-GB" outputs (replace()
to replace the "bars")
let UNIX = Date.now()
console.log('UnixTimeStamp: ', UNIX)
console.log('Unix String (UTC): ', new Date(UNIX))
function Format(timestamp, lang) {
let dateObj = new Date(timestamp)
return dateObj.toLocaleString(lang, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute:'2-digit',
second:'2-digit'
}).replace(/\//g, '-')
}
console.log('formato pt-BR: ', Format(UNIX, 'pt-BR'))
console.log('formato en-US: ', Format(UNIX, 'en-US'))
console.log('formato en-GB: ', Format(UNIX, 'en-GB'))
The above example only displays UNIX-based formatting, it does not express the difference between Timezones.
In open systems such as posts (blogs) it is very common to have users from various parts of the world, for example saving the already formatted date of a user’s publication in the bank, how to show other users "scattered" around the world the date of the post (in a fluid manner in their languages and schedules)? Saving the date of publication in Unixtimestamp this is possible:
let UNIX = Date.now()
console.log('UnixTimeStamp: ', UNIX)
console.log('Unix String (UTC): ', new Date(UNIX))
function Format(timestamp, lang, tz) {
let dateObj = new Date(timestamp)
return dateObj.toLocaleString(lang, {
timeZone: tz,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute:'2-digit',
second:'2-digit'
}).replace(/\//g, '-')
}
console.log('formato pt-BR: ', Format(UNIX, 'pt-BR','America/Sao_Paulo'))
console.log('formato en-US: ', Format(UNIX, 'en-US', 'America/New_York'))
console.log('formato en-GB: ', Format(UNIX, 'en-GB', 'Europe/London'))
The above example uses the option Timezone of toLocaleString()
to simulate the local Timezone of possible users around the world, in this case, a Paulista a "Novayorkino" and a Londrino.
As countries may have more than one Timezone in a real environment the correct would be to take the user’s local Timezone (which is seeing/accessing a publication):
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
This way users "scattered" around the world can see exact date of when another user made a post taking into account their local time.
NOTE:
The best way to work with dates for formatting (internationalization) is to use Unixtimestamp references because they can be formatted for any output. When using already formatted strings you will have trouble converting (formatting) to other outputs.
References
Unixtimestamp: RFC2822, ISO8601 section 15.9.1.15
Javascript object Date()
: Mozila Docs
Javascript Intl.DateTimeFormat()
: Mozila Docs
Javascript toLocaleString()
: Mozila Docs
Language tags used in toLocaleString()
follow the BCP47
Complemetos:
Remove hours, it’s only the date
– Matheus Miranda
Example:
moment(testDate).format('YYYY-MM-DD');
– Matheus Miranda
if the warning bothers you why not just use console.clear()? If not you will end up falling into the problem of using a shotgun to kill ant
– Paz
sorry my datepicker uses hours, should have put there
– Leandro RR
@Peace because when I do the parse it gives the warning and returns me
invalid date
– Leandro RR
@Leandrorr, nothing wrong, it works perfect here: http://jsfiddle.net/r42jg/2668/
– Matheus Miranda
Possible duplicate of How to format date in javascript?
– Caique Romero
@Caiqueromero your comment is wrong, Leandro’s question is about formatting date with the Moments library.
– Matheus Miranda
@Caiqueromero is not duplicate because the question you mentioned is doing the opposite, from English to Portuguese.
– Leandro RR
See the @Caiqueromero reply, and see if you solved the problem, don’t forget to accept the answer as accepted.
– Matheus Miranda
@Leandrorr seems to me that you are trying to internationalize your bad software/ system, the format YYYY-MM-DD is not common for English language users (en-US) who follow (use) the format MM/DD/YYYY. I believe that if the goal is to "internationalize" the software should follow the standard language format.
– Lauro Moraes
@Lauromoraes the format I am trying to parse, is to store in my SQL Server database.
– Leandro RR