This question was already asked today but the owner seems have deleted it(even it has 4 up vote). However the question was so interesting, I have decided to post by my self again.
I have an object in Javascript which has one property with a date string.
Now I want set a new datetime to that property, but how can I set the new data without knowing its format?
The sample datetime looks like this "2018-01-01T20:09:00"
This question can be divided in 2 answer.
Identify the current mentioned format & set the the same format to object property.(This seems to be achieved easily if someone say what type of datetime format is this)
Identify some generic solution that determine any datetime format & convert given datetime to set object property.
How to determine date format from string? 1 Identify the current mentioned format & set the the same format to object property.(This seems to be achieved easily if... 2 Identify some generic solution that determine any datetime format & convert given datetime to set object property. More ...
Here, the strDate string is in the format “dd/MM/yyyy” which is needed to be parsed to create a new Date object with the exact components (day, month and year). This is referred as date parsing. One also needs to display a Date object as a String in a specified date pattern. This is referred as date formatting.
You will need to take the inital date string and covert it to a date object and pass that converted date object and format it to your required string. Show activity on this post. You could try dateparser. It can recognize any String automatically, and parse it into Date, Calendar, LocalDateTime, OffsetDateTime correctly and quickly ( 1us~1.5us ).
The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern. The LocalDate class represents a date-only value without time-of-day and without time zone. Generate a String in the same format by calling toString. To parse/generate other formats, use a DateTimeFormatter.
If you can use moment.js then the following does the job:
var dateFormats = {
"iso_int" : "YYYY-MM-DD",
"short_date" : "DD/MM/YYYY",
"iso_date_time": "YYYY-MM-DDTHH:MM:SS",
"iso_date_time_utc": "YYYY-MM-DDTHH:MM:SSZ"
//define other well known formats if you want
}
function getFormat(d){
for (var prop in dateFormats) {
if(moment(d, dateFormats[prop],true).isValid()){
return dateFormats[prop];
}
}
return null;
}
var dateInput = "2018-01-01T20:09:00";
var formatFound = getFormat(dateInput); //returns "YYYY-MM-DDTHH:MM:SS"
if(formatFound !==null){
//do stuff
}
Check the moment js docs for more info on the supported dateFormats by default and populate your dateFormats object with them.
Tl;Dr
There's a lot of snake oil getting sold here.
Date formats are not mutually exclusive, two (or more) formats may look the same but the values represent different parts of a date. This makes it practically impossible to determine the date format from a string.
For example, 01/02/2022
is 1st Feb 2022 in UK format and 2nd Jan 2022 in US format. So given the string 01/02/2022
you cannot determine whether this is uk or us format.
Given an imaginary method returnFormat("01/02/2022")
what would you expect the response to be here? It might return en-US or en-GB or an of the other overlapping formats.
Date formats were not designed with computer parsing in mind. The only date format designed for computers is a ISO 8601 format (e.g. 2008-05-11T15:30:00Z
) which can include the UTC offset. so times with a Z
are utc and +01:00
would be UTC +1. This format is specifically chosen so as not to clash with any human readable formats for the problems outlined above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With