I have a date/time string like 2012-01-13 04:37:20
but I want to convert it to dd-mm-yyyy hh:mm
, how can i do this?
I am using the followin
I think it is best to use the Intl.DateTimeFormat
class.
The usage is fairly straightforward. You can not enter a pattern as you want to, but it will give you the results you want.
Here is an example on how to use it:
public formatDate(date : Date) : string{
var options = { year: 'numeric', month: 'short', day: 'numeric' };
return new Intl.DateTimeFormat('de-DE', options).format(date);
}
If you really want to enter a DateTimeFormat string, it would be easy enough to write a function which parses the string using Regex, but I don't think it is needed.
For further Information go here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat