Format Date String in momentjs - date

I am trying to format a timezone based
How can i convert a JS time into these formats?
"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
"September 24th 2015, 2:00:00 pm UTC-07:00"
"2015-09-24 14:00:00 GMT-0700"
"Sept 24 2015 14:00:00 GMT-0700"
"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
Converting into any of it would help.

You can use tokens listed in the format documentation, as shown in the following snippet.
Use square brackets [] to add characters that should be escaped (GMT and UTC in the example, if you need current zone abbreviation use the z token).
Note that as the moment-timezone docs says:
Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.
To provide long form names, you can override moment.fn.zoneName and use the zz token.
You can find in the snippet an example of providing long names for zones.
var time = "2016-11-09 15:38:00", zone = "America/Chicago";
var m = moment.tz(time,zone);
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (z)'));
console.log(m.format('MMMM Do YYYY, h:mm:ss a [UTC]ZZ'));
console.log(m.format('YYYY-MM-DD HH:mm:ss [GMT]ZZ'));
console.log(m.format('MMM D YYYY HH:mm:ss [GMT]ZZ'));
// Add long names for sample zones
var abbrs = {
EST : 'Eastern Standard Time',
EDT : 'Eastern Daylight Time',
CST : 'Central Standard Time',
CDT : 'Central Daylight Time',
MST : 'Mountain Standard Time',
MDT : 'Mountain Daylight Time',
PST : 'Pacific Standard Time',
PDT : 'Pacific Daylight Time',
};
moment.fn.zoneName = function () {
var abbr = this.zoneAbbr();
return abbrs[abbr] || abbr;
};
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (zz)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>

I was able to make third one using this snippet
var time = "2016-11-09 15:38:00",
zone = "America/Chicago",
format = "YYYY-MM-DD HH:mm:ss zZZ";
moment.tz(time,zone).utc().format(format)

Related

Logstash: How to match the timezone ID 'CET' in a date filter pattern?

In Logstash, I want to convert a string into a timestamp using the date filter. The string looks follows:
Fri Nov 05 06:24:28.651 CET 2021
I've tried the following pattern to no avail:
date {
match => [ "syslog_timestamp", "EEE MMM dd HH:mm:ss.SSS ZZZ yyyy"]
locale => "en_US"
timezone => "Europe/Berlin"
target => "syslog_timestamp"
}
This is confusing since Logstash is said to use the Joda library and Joda in turn says 'CET' is a legal timezone ID. I confirmed the results by testing the Jody library v2.10.13 directly in a Java application.
How to parse CET/CEST in the date filter?
Since time zone names (z) cannot be parsed and ZZZ still wouldn't match the daylight-saving variant 'CEST' according to Joda's documentation, I worked around this issue in Logstash by handling the timezone code as text and passing multiple patterns with the standard time zone and daylight-saving time zone to the filter:
match => [ "syslog_timestamp", "EEE MMM dd HH:mm:ss.SSS 'CET' yyyy", "EEE MMM dd HH:mm:ss.SSS 'CEST' yyyy"]

Truncate date to start of the day in a given timeZone

I am looking to truncate the date time to start of the day for the given timeZone.
If the current time is Mon Aug 24 15:38:42 America/Los_Angeles, it should be truncated to start of the day Mon Aug 24 00:00:00 America/Los_Angeles which then later should be converted to equivalent UTC time.
I have explored the methods provided by Joda Time Library, Apache Commons Library and ZonedDateTime but all of them truncate the date time in UTC and not to specific timeZone.
Can someone help me with this? Thanks in advance.
You can use ZonedDateTime. Use toLocalDate() on ZonedDateTime to get LocalDate then atStartOfDay on LocalDate with zone of ZonedDateTime instance to get the start of day.
Example:
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = now.toLocalDate().atStartOfDay(now.getZone());
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(now.format(formatter)); // Mon, 24 Aug 2020 10:41:41 -0700
System.out.println(startOfDay.format(formatter)); // Mon, 24 Aug 2020 00:00:00 -0700
ZonedDateTime truncates in its own time zone. So it can be done a bit simpler than in the currently accepted answer (which is also a good and correct answer).
ZonedDateTime given = ZonedDateTime.of(2020, 8, 24, 15, 38, 42, 0, ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = given.truncatedTo(ChronoUnit.DAYS);
System.out.println("Truncated to start of day: " + startOfDay);
Instant inUtc = startOfDay.toInstant();
System.out.println("In UTC: " + inUtc);
Output is:
Truncated to start of day: 2020-08-24T00:00-07:00[America/Los_Angeles]
In UTC: 2020-08-24T07:00:00Z

Change the format of a google spreadsheet date to include day of the week

I am trying to change a simple date format "1/24/2016" to " Tuesday, Jan 24 2016"
sheet.getRange("A2:A10").setNumberFormat("EEE, d MMM yyyy");
prints:
"EEE, Jan 24 2016"
This is an undocumented feature
you can use:
range.setNumberFormat("DDD, MMM dd, YYYY");
For more details you can see the issue report here:
https://code.google.com/p/google-apps-script-issues/issues/detail?can=2&start=0&num=100&q=setNumberFormat&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner&groupby=&sort=&id=4175

How to parse these time zones with NSDateFormatter?

I'm parsing a large number of internet dates. First I try a formatter with en_US_POSIX locale, then with en_GB. The code looks more or less like this:
{
NSDate *date = [dateString dateWithDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z (zzz)" localeIdentifier:#"en_US_POSIX"];
if (date) return date;
date = [dateString dateWithDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z (zzz)" localeIdentifier:#"en_GB"];
return date;
}
- (NSDate*) dateWithDateFormat:(NSString*)dateFormat localeIdentifier:(NSString*)localeIdentifier
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
formatter.dateFormat = dateFormat;
[formatter dateFromString:self];
}
However, date strings with the following time zones fail to parse:
Mon, 16 Jul 2012 12:08:17 +0100 (GMTUK)
Thu, 6 Sep 2012 13:00:06 +0900 (KST)
Wed, 3 Nov 2010 10:12:15 +0100 (Hora est�ndar romance)
Wed, 14 Sep 2011 14:37:35 +0100 (IST)
Wed, 2 May 2012 09:41:06 +0200 (MEST)
Sun, 31 Oct 2010 12:53:06 +0800 (SGT)
Thu, 19 Jan 2012 08:34:44 -0300 (UYT)
What am I doing wrong?
Should I pre-process the strings to remove the time zone parenthesis in these cases only?
NSDate can store a point in time without timezone information. It's up to your software to know whether a specific NSDate instance stores the point in time in UTC or in the local time zone. In most cases, you want to use UTC dates.
Because of that, it's important to handle time zone differences when parsing the dates. But it's not possible to remember the time zone the dates was originally in (at least not with an NSDate instance only).
So I would recommend that you cut off the time zone in parenthesis and just parse the numerical time zone offset before it. That way, you can convert all strings into an NSDate instance in UTC and you shouldn't have any problems parsing the strings.
And shouldn't the date fromat be (i.e. uppercase Z for a numeric time zone offset)?
#"EEE, dd MMM yyyy HH:mm:ss ZZZ"

Date Formatting in Objective C (iOS SDK)

Hope you can help. I am importing the current GMT time for an iPhone App. This is being retrieved via a JSON web service.
I believe I have the correct formatter string however I am getting a different date (time is still correct) when I try to format the date I've retrieved. The JSON date is formatted like this: Sun, 15 May 2011 20:35:31 +0000
In the example below strGMT is the date in the format I've just mentioned.
This is the code I'm using to get retrieved date into my code:
NSLog(#"Current GMT: %#", strGMT);
NSDateFormatter *gmtFormatter=[[NSDateFormatter alloc] init];
[gmtFormatter setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss VVVV"];
//THIS IS NOT REFORMATTING CORRECTLY HERE
NSDate *gmtDateTime=[gmtFormatter dateFromString:strGMT];
NSLog(#"Current Formatted GMT Date: %#", gmtDateTime);
The log is showing the following:
Current GMT: Sun, 15 May 2011 20:35:31 +0000
Current Formatted GMT Date: 2010-12-26 20:35:31 +0000
Have I not got the formatting string correct? Any ideas why it's gone from 15 May 2011 (today) to 26th December 2010?
Kind regards
Paul
The correct format string is #"EEE, dd MMMM yyyy HH:mm:ss VVVV"