Reputation: 337
When I am using new Date, I am getting something like as follows:
Wed Mar 21 2018 16:14:50 GMT+0530 (India Standard Time)
but what I want is xxxxxxxxxxxxxxxx
/(21032018041217PM
) formatted time string
Here(21032018041217PM
) is 21 is date, 03-month, 2018-year, 04-time, 12-minutes, 17-seconds
and It should be AM/PM
.
Upvotes: 0
Views: 234
Reputation: 2038
var date = new Date();
var dateFormat = (date.getDate().toString().length == 1? "0":'' ) + date.getDate() + (date.getMonth().toString().length == 1? "0":'' ) + date.getMonth() + "" + date.getFullYear()
var hours = ((date.getHours()%12).toString().length == 1?'0':'') + "" + (date.getHours()%12);
var minuts = ((date.getMinutes()).toString().length == 1?'0':'') + "" + (date.getMinutes());
var seconds = ((date.getSeconds()).toString().length == 1?'0':'') + "" + (date.getSeconds());
var format = (date.getHours() >= 12 && date.getHours()%12 != 0) ? 'PM':'AM'
var yourDate = dateFormat + hours + minuts + seconds + format
Upvotes: 1
Reputation: 337
I'm using like this,
date.getDate()+""+(date.getMonth() + 1)+""+date.getFullYear()+""+ date.getHours()+""+date.getMinutes() +""+date.getSeconds();
but this formate of the result 2132018163431
i'm getting
Upvotes: 0
Reputation: 786
You could use Moment.js to achieve your desired format:
console.log( moment().format('DDMMYYYYhhmmssA') )
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
Upvotes: 2
Reputation: 1768
You can handle this quite easily by using momentjs
library. The documentation is quite detailed here: https://momentjs.com/docs/#/displaying/
It will allow you to handle date formatting any way you desire.
You can download it from here: https://momentjs.com/
Without using momentJS
library:
To convert the current date and time to a UNIX timestamp do the following:
var ts = Math.round((new Date()).getTime() / 1000);
getTime() returns milliseconds from the UNIX epoch, so divide it by 1000 to get the seconds representation. It is rounded using Math.round() to make it a whole number. The "ts" variable now has the UNIX timestamp for the current date and time relevent to the user's web browser.
You can also take a look here: How do you get a timestamp in JavaScript?
Upvotes: 1
Reputation: 162
I would suggest moment.js otherwise you can do string concatenations and calculations be careful month is 0-11 which means 3 might actually be 04 not 03.
Upvotes: 0