0

I have a variable containing timestamp ( in seconds generated with php ), I used following code to create date object using javascript

var newdate = new Date(arrivalDate);

then used following code to get formated date in utc

var tempdate = newdate.getUTCFullYear()+'-'+('0' + (newdate.getUTCMonth()+1)).slice(-2)+'-'+('0' + newdate.getUTCDate()).slice(-2);

It returns me a 1 day before date, not sure how to get utc date from a timestamp.

9
  • 1
    are you sure that the time in your local timezone isn't just a time during the previous day in UTC? Commented Apr 3, 2018 at 12:39
  • I am 6 hours ahead of utc ! Commented Apr 3, 2018 at 12:40
  • that means 2nd of April : 12:00:00 (midnight) in your time zone is actually 1st of April 18:00:00 (6pm) in UTC so the code behaves correctly . How do you save your date? Maybe you should send the date in UTC when you save it. Commented Apr 3, 2018 at 12:41
  • May b I wrote it wrong but its 6am on my local machine when it 12 am at utc Commented Apr 3, 2018 at 12:43
  • yes, it should be. Commented Apr 3, 2018 at 12:44

1 Answer 1

2

var newdate = new Date(arrivalDate); newdate.toUTCString();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.