I am working on a project and I get the following string representation of time and date, i.e. "00:02:52 APRIL 11, 2013 GMT". When I use the DateTime.Parse() method:
DateTime dt= DateTime.Parse(dateString);
string text = dt.ToString("hh:mm:ss MMM dd, yyyy ").ToUpper();
My output (text) is:
"05:02:52 APR 11, 2013 GMT"
and NOT
"00:02:52 APR 11, 2013 GMT"
I don't get why the hour (HH) has changed to 00 to 05. I traced my code through V.S. many times. All I am doing is truncating the month, APRIL to APR.
Would anyone please give me insight on what I'm doing wrong or missing?
Thanks!
Try this
string dateString = "00:02:52 APRIL 11, 2013 GMT";
DateTime dt = DateTime.Parse(dateString).ToUniversalTime();
Related
Given a (UTC) DateTime object, how can I get the corresponding time in Berlin, in CET or CEST, depending on the date?
Examples for the desired "convert" function:
convert(DateTime.new(2018, 07))
=> Sun, 01 Jul 2018 02:00:00 +0200
convert(DateTime.new(2018, 12))
=> Sat, 01 Dec 2018 01:00:00 +0100
Try this
require 'tzinfo'
timezone = TZInfo::Timezone.get('Europe/Berlin')
local_time = timezone.utc_to_local(utc_time)
That even works without Rails.
I would like to say if now is less than Jan 1st at midnight Pacific Standard Time. I thought:
DateTime.new(2017,01,01, 00, 00, 00).in_time_zone('Pacific Time (US & Canada)')
would do it but it gives me back?
Sat, 31 Dec 2016 16:00:00 PST -08:00
Should I be using?
DateTime.new(2017,01,01, 8, 00, 00).in_time_zone('Pacific Time (US & Canada)')
or what is the proper, Rails-approved way of saying a specific day / time in Rails for a specific time zone?
You can specify the timezone when creating the datetime, i.e:
datetime = DateTime.new(2017,01,01, 00, 00, 00, 'PST')
#=> Sun, 01 Jan 2017 00:00:00 -0800
datetime.utc
#=> Sun, 01 Jan 2017 08:00:00 +0000
I'm trying to parse a specific hour of a specific date. When I put the date directly as an argument, it works fine, but when I create a variable and put it in the argument it returns the current date.
Why is that?
NOTE: the variable time is 9pm and I need to parse 9pm of 12 March 2016.
datetime = DateTime.new(2016,3,12,9)
=> Sat, 12 Mar 2016 09:00:00 +0000
DateTime.parse("sat 12 march 2016 9pm")
=> Sat, 12 Mar 2016 21:00:00 +0000
DateTime.parse("datetime 9pm")
=> Mon, 14 Mar 2016 21:00:00 +0000
In your third call, you use the literal string "datetime" rather than the value of your datetime variable. You can use string interpolation to use the variable's value:
DateTime.parse("#{datetime} 9pm")
In this case, the "9pm" is ignored since it doesn't make sense added to the end of an existing date but this is why the initial attempt wasn't working. Interpolation is generally a solution for using a variable's value rather than its name.
If your goal is to change the time of an existing date, use the change method:
datetime.change(hour:21)
You can also try this
date = Date.new(2016,3,12)
DateTime.parse("#{date} 9pm")
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
datetime = DateTime.new(2016,3,12,9)
DateTime.parse((datetime + 12.hours).to_s)
## Output
Sat, 12 Mar 2016 21:00:00 +0000
OR
DateTime.parse((datetime + 12.hours).to_s).strftime("%a, %d %b %Y %I:%M %p")
## Output
Sat, 12 Mar 2016 09:00 PM
EEE, dd MMM yyyy HH:mm:ss zzz is the date formatter used in my application . The I run this on iOS9 the date string returned is Fri, 27 Nov 2015 16:38:47 IST and in iOS8 its Fri, 27 Nov 2015 16:44:04 GMT+5:30. Anyone whose how to solve this.
How do I pass date in RFC2822 format, to an URL in GET method:
e.g I want to pass 18th jan 2013 as min_date_created in the URL https://www.xyz.com/orders
18th jan 2013 in RFC format is ==> Fri, 18 Jan 2013 17:58:49 +0000
how can I pass it to URL?
TIA!
Instead of passing the date as a string, you could convert it to the number of milliseconds since midnight Jan 1, 1970:
var time = new Date('Fri, 18 Jan 2013 17:58:49 +0000').getTime(); // 1358531929000
widow.location.href = 'https://www.xyz.com/orders?min_date_created='+time;
This way you won't have encoding problems.
Or you could also use encodeURIComponent(), see URL encode sees “&” (ampersand) as “&” HTML entity