Convert "Sat, 27 Apr 2013 08:54:17 EDT" to NSDate - nsstring

Can somebody help in converting the text "Sat, 27 Apr 2013 08:54:17 EDT" into String. I have tried the NSDateFormatter with formatting style of "EEE, dd MMMM yyyy HH:mm:ssZ" but it returns only nil value. I am able to convert valeswhich have EST, GMT but facing problem in EDT.

[NSTimeZone knownTimeZoneNames];
This will return all the known timezones. You can check if EDT is there. I think it is there. Also, in the date format, you gave MMMM. It means named month (like january, february). So in your string, try giving April instead of Apr. Also, you can try giving z instead of Z.

Related

Integromat date parser

How can I parse a Text Field with date information to a real date?
Here is an example string: Thu, Jan 28, 10:00 AM CEST
You can use Integromat's Date & Time Functions: parseDate to achieve this. For your question using format ddd, mmm dd, HH:MM tt zzz should work.

How to getValue() on a date with the correct TimeZone?

Someone in the "Europe/London" (UTC +0) Timezone created a Spreadsheet. They input the date "March 22 2019" into one of the fields. This represents the date 3/22/2019 0:00 UTC+O.
I'm in the "America/Los_Angeles" (UTC -7) Timezone. When I run a Google Apps Script and try to get the value, it's evaluated as:
Thu Mar 21 17:00:00 GMT-07:00 2019
This is accurate since 3/22/2019 0:00 UTC+O == 3/21/2019 17:00 UTC-7.
The problem is when I try to get the date programmatically, I'll get it as 21 instead of 22.
For example, if I try to run:
cellRange.getValue().getDate() // returns 21 even though the sheet shows the date as 22
This is because getDate() returns values "according to local time." The local script time is UTC-7, hence it's returning 21.
However, this causes a dissonance:
Spreadsheet date: 22 (i.e. the value I see in the sheet)
Programmatic date: 21 (i.e. the one returned by getDate() above)
This is problematic when I'm trying to use a function such as joinDateAndTime_() with "March 22 2019" as the date and 6am as the time, for example. This causes it to produce a date "March 21 2019 6:00 UTC-7" instead of "March 22 2019 6:00 UTC-7".
What is the best solution here?
Not a complete solution (I'll update soon)
It seems like this is what is happening:
The value is hard-coded as "March 22" (text).
When a user opens the sheet, no matter what timezone they are in, it'll assume it represents March 22 in the sheet's timezone. 3/22/2019 0:00 UTC+0
Once you read the value into a JavaScript Date, all date functions assume you want it in your current (aka the script's) timezone. 3/21/2019 17:00 UTC-7
Solution A: Just add the hours
Forget about the timezones. Instead of hardcoding the hours in a Date, just offset the date by the hours you want.
The only downside is you need to be certain that the date started at 0:00 according to whatever timezone it was in. (E.g. if they decided to write "March 22 2019 5:00", then you'll be offsetting the hours incorrectly.)
Solution B: Do some math
I'll update this soon, but eventually you might want a function sheetTimezoneOffset() that could be used like this:
function getDate(cellRange) {
var date = cellRange.getValue().getDate();
var extraneousHours = formatDate(date, "h", sheetTimezoneOffset());
date = date.addHours(-extraneousHours);
var offsetHours = 6; // e.g. for 6am
date.addHours(offsetHours);
return date;
}

Random TMZ on a dateTime [Spreadsheet]

I have a starting time in "HH:MM:SS" format. In my spreadsheet, the user enters it in this format, like "09:00:00" for 9AM. When I get this cell's value in my script, I get this weirdo :
"Sat Dec 30 09:00:00 GMT+00:09 1899"
It seems like I have that random date of Sat Dec 30 1899, which I don't really care about, but also the "GMT+00:09", that bothers me a lot more. My spreadsheet and script are "GMT+01:00 Paris", so when I insert this data elsewhere, I get 09:50 GMT+01:00... Not even 51 minutes off, as I would have expected.
Do you have any idea how and why it happens ?
The fix I use right now is to display the time in my "HH:MM:SS" format, or "HH:MM", but set the value to, say "05/01/2018 09:00:00", so it is considered a full dateTime in my timezone. I would really like to be able to only specify the time here.
Thanks for your answers,
How about this answer? This answer supposes the following condition.
Format of cell "A1" is HH:MM:SS for time.
Value of cell "A1" is 09:00:00.
In your 1st case :
When the value of "A1" is retrieved by getValue(), the value is Sat Dec 30 09:00:00 GMT+09:00 1899. (I'm sorry. "GMT+09:00" is due to the time zone. My time zone is Asia/Tokyo.) Because Spreadsheet uses Serial Number for Date/Time. The start of serial number is 12/30/1899 00:00:00. In this case, because there are no date information, Sat Dec 30 09:00:00 GMT+09:00 1899 is retrieved as the value which elapsed for 09:00:00 from the start.
If you want to retrieve 09:00:00 from cell "A1", please use getDisplayValue() instead of getValue(). By this, you can retrieve 09:00:00.
In your 2nd case :
I think that this is better usage. 05/01/2018 09:00:00 is imported to the cell with the format of HH:MM:SS. By this, the value has the information of both date and time. So the values of cell retrieved by getValue() and getDisplayValue() are Fri Jan 05 09:00:00 GMT+09:00 2018 and 09:00:00, respectively.
Sample :
In the case using getValue()
SpreadsheetApp.getActiveSheet().getRange("A1").getValue();
In the case using getDisplayValue()
SpreadsheetApp.getActiveSheet().getRange("A1").getDisplayValue();
References :
getValue()
getDisplayValue()
Date/Time serial numbers
If I misunderstand your question, I'm sorry.

convert another timezone time to local time in momentjs

I want to convert Fiji(Pacific/Fiji) time to my local time.
suppose,
I have Fiji(Pacific/Fiji) time "Thu, 10 November 2016 03:47" PM and I am in India then convert it in India(Asia/Kolkata) time "Thu, 10 November 2016 08:17 AM".
so, question is
which time string need for Fiji? and how to convert it to local time in moment.js?
need to UTC string like 2016-11-16T06:30:00.000Z
working fiddel here:
http://jsfiddle.net/d06047c5/

how to convert time stamp 07 Mar 2016 01:00:03 PM to numeric in r

Im trying to find time difference between two timestamps, say, start and endtime. But the stamps are in string format :07 Mar 2016 01:00:03 PM. Can someone help how to convert this in numeric and find the time difference? Thanks
lubridate is your new best friend so download it right away to get to know her.
install.packages('lubridate')
library(lubridate)
Based on your example, use dmy_hms()
a <- dmy_hms('07 Mar 2016 01:00:03 PM')
# "2016-03-07 13:00:03 UTC"
b <- dmy_hms('08 Mar 2016 01:00:03 PM')
# "2016-03-08 13:00:03 UTC"
Then a simple subtraction could get the date diff.
b - a
# Time difference of 1 days
Or you can also use the as.numeric() use this number for calculation later.
as.numeric(b - a)
# 1

Resources