-1

I am new to JS and it may be a simple query but I have searched a lot for this and did not get expected result. My query is that How can I convert seconds to (DD/MM/YYYY H:M:S AM/PM) format in javascript.

I am getting below as response for time 1535004239

Please help me in this. Thanks in Advance

Chanchal S
  • 29
  • 4

2 Answers2

0

you can do something like this using Date

This solution remove all timezone, every date is rapported to UTC

formatted use 24H format instead of AM/PM

let value = 1535004239
// multiply by 1000 to get milisecond
console.log(new Date(value * 1000))

let formatted = (new Date(value * 1000)).toISOString() 
formatted = formatted.replace("T", " ").replace(/\.?[0-9]*Z/, "")

console.log(formatted)

for 12H format (may not be correct as I never use it)

function getFormattedDate(value) {
  let date;
  if (value) {
    date = new Date(value * 1000)
  } else {
    date = new Date()
  }
  let suffix = "AM"
  if (date.getUTCHours() > 12 || date.getUTCHours() === 0) {
    date.setUTCHours(date.getUTCHours() - 12)
    suffix = "PM"
  }

  let formatted = date.toISOString()
  formatted = formatted.replace("T", " ").replace(/\.?[0-9]*Z/, "") + " " + suffix

  return formatted;
}

console.log(new Date(1535004239 * 1000).toISOString() + " : " + getFormattedDate(1535004239))
console.log(new Date(1535148239 * 1000).toISOString() + " : " + getFormattedDate(1535148239))
console.log(new Date(1535030639 * 1000).toISOString() + " : " + getFormattedDate(1535030639))
console.log(new Date(1535028639 * 1000).toISOString() + " : " + getFormattedDate(1535028639))
console.log(new Date(1535155600 * 1000).toISOString() + " : " + getFormattedDate(1535155600))
console.log(new Date().toISOString() + " : " + getFormattedDate())

If you can use any format you want prefer the default Date.toString as it gives informations about time zone

jonatjano
  • 3,576
  • 1
  • 15
  • 20
  • Thanks for the reply... can't we make it to 12 hrs format as my expected output is 23/08/2018 11:08:35 AM And my current output after applying changes suggested above by you is : 2018-08-23 06:03:59 – Chanchal S Aug 28 '18 at 09:15
  • @ChanchalS edited. about the difference beetwen the date are your sure about your timestamp being `23/08/2018 11:08:35` ? because that doesn't seem possible since the last digit is the second digit of the seconds – jonatjano Aug 28 '18 at 09:47
0

The Date Object is always at the rescue. You are receiving the value 1535004239 in seconds. So first you need to convert it to milliseconds.

var currentDate=1535004239*1000

Next, you can pass the value as directly to the Date constructor.

var date=new Date(currentDate)

Thanks to some certain predefined functions of the Date object in ES5, you can now get your formatted Date using the .toLocaleString() function

var dateString=date.toLocaleString()
console.log(dateString)

The code above should output something like

23/08/2018, 11:33:59

Edit 1: Use the code below to exactly get your formatted output

Once you have the date in the date variable, to get the exact format you want, use the code below

var currentDate= date.toLocaleDateString()+' '+date.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second:'numeric', hour12: true })

This should output something like 23/08/2018 11:33:59 AM

Pang
  • 9,564
  • 146
  • 81
  • 122
Harshil Shah
  • 83
  • 1
  • 8
  • Thanks Harshil .. Just help me more here by knowing that => how can I remove the comma => how can I make time as 12 hours format ? Please let me know – Chanchal S Aug 28 '18 at 09:48
  • @ChanchalS please check out the latest edit. Should clear all your queries. – Harshil Shah Sep 04 '18 at 06:05