If a date format is $scope.timestamp = '2016-12-16 07:02:15 am'
I want to format to 16/12/2016 07:02:15 am
I have tried this below code and it's working good
$scope.originalStamp = $filter('date')
(new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');
But my question is why
new Date($scope.timestamp)
is always returnnull
if am not use replace the char from (-) to (/)?
see the below code is not working if am without using replace().
$scope.originalStamp = $filter('date')
(new Date($scope.timestamp),'dd-MM-yyyy HH:mm:ss a');
Why the
new Date()
does not accept if the date format having (-)? Is new Date() conversion is depend by my system date format?
If a date format is $scope.timestamp = '2016-12-16 07:02:15 am'
I want to format to 16/12/2016 07:02:15 am
I have tried this below code and it's working good
$scope.originalStamp = $filter('date')
(new Date($scope.timestamp.replace("-","/")),'dd-MM-yyyy HH:mm:ss a');
But my question is why
new Date($scope.timestamp)
is always returnnull
if am not use replace the char from (-) to (/)?
see the below code is not working if am without using replace().
$scope.originalStamp = $filter('date')
(new Date($scope.timestamp),'dd-MM-yyyy HH:mm:ss a');
Share edited Jan 31, 2017 at 9:14 Ramesh Rajendran asked Jan 11, 2017 at 5:14 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges159 silver badges240 bronze badges 9Why the
new Date()
does not accept if the date format having (-)? Is new Date() conversion is depend by my system date format?
- 2 You should not use the Date constructor to parse strings at all. Write a small function or use a library. It should not be returning null but an invalid Date (i.e. a Date object with time value of NaN) that might be interpreted later as null. – RobG Commented Jan 11, 2017 at 5:25
- Did you try 01-01-2016? – Ataur Rahman Munna Commented Jan 11, 2017 at 5:27
-
'yyyy-MM-dd HH:mm:ss a'
, not'dd-MM-yyyy HH:mm:ss a'
– Hezron Commented Jan 11, 2017 at 5:28 - 1 @AtaurRahmanMunna—did you read the OP? – RobG Commented Jan 11, 2017 at 5:28
- 1 @RameshRajendran—in implementation may decide that "2016/12/16" is an invalid attempt at an ISO 8601 date string and return NaN, or it might decided that it's not ISO 8601 and fall back to whatever heuristics it wants, perhaps returning a date for 16 December, 2016. Both behaviours are consistent with ECMA-262. – RobG Commented Jan 11, 2017 at 5:42
1 Answer
Reset to default 6Why the new Date() does not accept if the date format having (-)?
Parsing of date strings other than the limited subset of ISO 8601 included in ECMA-262 is implementation dependent. You may get the correct result, an incorrect result or an invalid date. Therefore it is strongly remended not to use the Date constructor or Date.parse to parse strings (they are equivalent for parsing), always manually parse strings with a library or bespoke function.
Is new Date() conversion is depend by my system date format?
No. There are a number of formats that are supported by convention (see MDN Date), however you should not rely on that as some are parsed, or parsed differently, in some browsers but not others.
You might try one of these libraries:
- Moment.js: parsing, formatting, arithmetic, manipulation
- Fecha.js: just parsing and formatting
PS
If you just want to reformat '2016-12-16 07:02:15 am' as '16/12/2016 07:02:15 am', then:
// Reformat a date like '2016-12-16 07:02:15 am' as
// '16/12/2016 07:02:15 am'
function reformatDate(s) {
var b = s.split(/[ -]/);
return [b[2],b[1],b[0]].join('/') + ' ' + b[3] + ' ' + b[4];
}
var s = '2016-12-16 07:02:15 am';
console.log(s + ' => ' + reformatDate(s))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744933016a4601851.html
评论列表(0条)