3

How to format date and time like this in JavaScript ?

March 05, 2012 @ 14:30 (UTC - 9:30)

I use this code to calculate EST time :

function getDate() {
    var now = new Date();
  var  utc = now.getTime() + (now.getTimezoneOffset() * 60000);

    return new Date(utc + (3600000 * -4));
}
3

3 Answers 3

4

I use the date-time-format that Tats recommended because doing it manually is a huge PIA.

var yourDate = dateFormat(getDate(), "mmmm dd, yyyy @ HH:MM) + "(UTC -9:30)";

Keep in mind this isn't Daylight Savings aware.. and you are asking for UTC -9:30 in your format, but your function converts to -4. Also, I believe that now.getTime returns in UTC.. so you can just add your difference there.

JavaScript Date Format

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

Comments

1

Check out date.js! It's a really powerful little library for working with Dates in JavaScript.

To get today's date in EST, you can do something like...

var today = new Date();
today.toString();         // outputs "Wed Apr 11 2012 15:40:40 GMT-0500 (CDT)"


today.setTimezone("EST");
today.toString();         // outputs "Wed Apr 11 2012 14:40:40 GMT-0500 (CDT)"

Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.

Comments

0

If you do just

var now = new Date();
document.write(now);

you will get

Wed Mar 14 2012 20:53:06 GMT+0000 (GMT Standard Time)

Link1, Link2.

Is it what you want?

2 Comments

I need EST time , I found my answer btw thanks for your attention.
@shaahin... can you post your solution?

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.