7

I'm looking for a suitable way to convert a timestamp from UTC to IST using JavaScript DateTimeStamp "20160108120040".

The timestamp comes from an XML in my body request.

Andrew Regan
  • 5,087
  • 6
  • 37
  • 73
Pramod SR
  • 73
  • 1
  • 1
  • 4
  • If you haven't noticed already, javascript is very bad at handling time and dates. They provide very little in the way of handling timezones and doing conversions. I usually just AJAX something to a server to be handled when needed. But you can also try [moment.js](http://momentjs.com/) I haven't used it but it comes recommended among my friends. – Squeegy Apr 07 '16 at 20:42
  • Thanks for your suggestion. Even moment.js i am not aware of it how to work. will check it and try to implement in my test script. – Pramod SR Apr 08 '16 at 09:29
  • Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – H. Pauwelyn Sep 22 '22 at 12:22

4 Answers4

12

First thing, take a look at JavaScript date formats and convert your input date accordingly, then you shoud take a look to the methods available in JavaScript for date manipulation (no external library required). It's pretty easy to do something like this:

var dateUTC = new Date("yourInputDateInASuitableFormat");
var dateUTC = dateUTC.getTime() 
var dateIST = new Date(dateUTC);
//date shifting for IST timezone (+5 hours and 30 minutes)
dateIST.setHours(dateIST.getHours() + 5); 
dateIST.setMinutes(dateIST.getMinutes() + 30);
Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
  • Notice that `dateIST` is not "an ist date", it's just a "wrong" date that when calling `.toUTCString()` on will yield a string that's accurate in IST. The date object should only be used for that `toUTCString()` call and not be used elsewhere. – Bergi Jan 06 '18 at 17:24
  • Helped me alot. Thanks :) – Pushprajsinh Chudasama Mar 13 '21 at 13:46
5

use toLocaleString and provide desired timezone:

new Date("yourInputDateInASuitableFormat").toLocaleString("en-US", {timeZone: 'Asia/Kolkata'})
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • 2
    The above code will give date in mm/dd/yy, hh:mm:ss format. to make it dd/mm/yyyy, hh:mm:ss format which is standard for indian time - use this code :- `new Date("yourInputDateInASuitableFormat").toLocaleString("en-IN", {timeZone: 'Asia/Kolkata'})` – aumiom Apr 05 '22 at 08:52
3
const getISTTime = () => {
  let d = new Date()
  return d.getTime() + ( 5.5 * 60 * 60 * 1000 )
}
Zooly
  • 4,736
  • 4
  • 34
  • 54
Prav
  • 610
  • 8
  • 20
0

Based on the accepted answer

export default class DateIST extends Date {
    constructor(params) {
        super(params)
        console.log(this)
        console.log(this.toString())
        console.log(this.toLocaleString())
        console.log(this.toISOString())
    }

    toISOString() {
        var date = new Date(this)
        date.setHours(date.getHours() + 5)
        date.setMinutes(date.getMinutes() + 30)
        return date.toISOString();
    }
}

var dateIST = DateIST('...')
console.log(dateIST.toISOString())

// For comparision purposes with Date, like when used in Mongoose Schema with ISODate in MongoDB
dbCollectionModel.find({ dateMongoDB: { $lt: dateIST.toISOString() } })
Himanshu Tanwar
  • 198
  • 1
  • 11