0

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.

Mahendra
  • 317
  • 2
  • 4
  • 16

5 Answers5

2

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>
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
Vladyslav
  • 786
  • 4
  • 19
  • here what is moment(), how to define? – Mahendra Mar 21 '18 at 10:58
  • It's time manipulations library. I have posted a link above. You can either download `moment.js` script file and include it in your project or add it via `npm` if possible. – Vladyslav Mar 21 '18 at 11:03
  • @JeremyThille Thanks, I'm new to this snippeting stuff. Perfaps I should examine it a bit soon :) – Vladyslav Mar 21 '18 at 11:12
  • @Jeremy, Here if i use this moment.min.js '' and if i run its getting error 'Refused to load the script 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".' – Mahendra Mar 21 '18 at 11:33
  • @Mahendra then just download the library and include the local version. – Vladyslav Mar 21 '18 at 11:35
  • @Vladyslav, https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/locale/az.js. Is this fine? – Mahendra Mar 21 '18 at 11:53
  • @Mahendra In case you need locale-specific version, just download it, put along with your sources and include it using ` – Vladyslav Mar 21 '18 at 11:57
  • No, az.js is the Azerbaidjan locale (plug-in). Just include moment.min.js – Jeremy Thille Mar 21 '18 at 12:00
  • @Vladyslav, can i get the link of moment.min.js library – Mahendra Mar 21 '18 at 12:10
1

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?

Nah
  • 1,690
  • 2
  • 26
  • 46
  • Your solution without `Moment.js` is wrong. You will receive simple timestamp (total number of seconds elapsed since 01/01/1970). It's not what the author is asking for. – Vladyslav Mar 21 '18 at 11:05
1
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
Mr.Pandya
  • 1,899
  • 1
  • 13
  • 24
0

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.

0

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

Mahendra
  • 317
  • 2
  • 4
  • 16
  • You just need to pad the numbers. `function pad(num,length) {return (new Array(length).join("0") + num).slice(-length);}` and then you can do `pad(date.getDate(),2) + pad(date.getMonth()+1,2) + pad(date.getFullYear(),4) + pad(date.getHours(),2) + pad(date.getMinutes(),2) + pad(date.getSeconds(),2);` – Niet the Dark Absol Mar 21 '18 at 11:13
  • Don't forget about ante/post meridiem (AM/PM) – Vladyslav Mar 21 '18 at 11:15