65

Possible Duplicate:
How do I Convert DateTime.now to UTC in Ruby?

How do I get current time in Date-time-milliseconds & UTC? Ex. 2012-03-22T18:48:40.873Z

I tried -

Time.now.utc_offset.to_s
Time.now.xs_datetime
Community
  • 1
  • 1
Majoris
  • 2,963
  • 6
  • 47
  • 81
  • 12
    `Time.now.utc` http://stackoverflow.com/questions/755669/how-do-i-convert-datetime-now-to-utc-in-ruby – ScottJShea Mar 27 '12 at 02:53
  • You want `Time.now.utc`. [This post][1] was the guide to my answer [1]: http://stackoverflow.com/questions/755669/how-do-i-convert-datetime-now-to-utc-in-ruby – ScottJShea Mar 27 '12 at 02:54
  • Got it - Time.now.utc.xmlschema – Majoris Mar 27 '12 at 02:54
  • A Ruby newbie like me might end up with this: `Time.utc(*Time.now.to_a)` – minopret Mar 27 '12 at 03:06
  • But how do I get those milliseconds? I need it exactly in this format and UTC - 2012-03-22T18:48:40.873Z – Majoris Mar 27 '12 at 03:08
  • @Firstname: Have a look at [`Time#strftime`](http://apidock.com/ruby/Time/strftime), which does exactly what you want. – Niklas B. Mar 27 '12 at 03:24
  • Thanks Nik. It is kind of confusing, how do I write for the format I need? – Majoris Mar 27 '12 at 03:28
  • @Firstname: It's all there in the documentation (along with a few examples), I won't write the format string for you (though it will probably start with `%Y-%m-%dT...`). Also for the future, if you want to ping people (so they get notified about your answer), use an @ character before the name :) – Niklas B. Mar 27 '12 at 03:38
  • How about an answer? Just that line of comment would give you at least 25 points :-) ... – mliebelt Mar 27 '12 at 06:09
  • @mliebelt, is right. Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%LZ") worked. But my bigger prob is still there. :/ [http://stackoverflow.com/questions/9881633/ruby-rails-savon-soap-digest-auth-issue#comment12603604_9881633](http://stackoverflow.com/questions/9881633/ruby-rails-savon-soap-digest-auth-issue#comment12603604_9881633) – Majoris Mar 27 '12 at 07:06
  • @mliebelt: Unfortunately I already passed the 200rep mark, so those 25 points would be wasted ;) – Niklas B. Mar 27 '12 at 12:39

1 Answers1

129

Time.now.utc is what you must be using.

hemanth.hm
  • 4,449
  • 2
  • 22
  • 10