I have looked at almost every post on here regarding NSDate conversion but have not been successful in resolving my issue.
Background:
I am wanting to simply compare NSDate (without time) values when rendering values within my CalendarView. However, when I convert the ISO date from the server it actually returns the previous date due to what I guess is GMT offset values. However, I just want to honor the Year, Month, Day which was passed from the server.
Code:
Value from the server: date: "2014-09-26T00:00:00.000Z"
Objective-C code to convert to NSDate. I am using Soffes library to handle the ISO date conversion.
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *date = [NSDate dateWithISO8601String:myDate timeZone:&timezone usingCalendar:nil];
NSDate *dateWithoutTime = [NSDate dateWithOutTime:date];
Code to return date without time:
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date];
return [[NSCalendar currentCalendar] dateFromComponents:comps];
Current Results:
ISO date from string
po date 2014-09-26 00:00:00 +0000
Date without time
po dateWithoutTime 2014-09-25 06:00:00 +0000
Any suggestions on how I can still honor the Year, Month, Day passed from the server?
Related
I am working at EST timezone. When I reading a date in Excel file with the help of ExcelJS. The date in Excel is 3/31/2021. But the same date is retrieving as Tue Mar 30 2021 20:00:00 GMT-0400 (Eastern Daylight Time). So it is considering the given date as UTC and converting it to my current timezone. I dont want to convert it to UTC and then to my current timezone. I want to read the direct date which is present in the EXCEL file.
I see dateUTC: false somewhere in the document. This is used at Writing the file. But how to do it when I am reading the excel file. Any help on this?
First I converted the GMT date to milliseconds using Date.parse() and added the time difference between GMT and the current time zone (in my case Asia / Tashkent +5) to the resulting value, then converted it back to a regular date using new Date(milliseconds)
let m = Date.parse(gmtDate); //GMT date in milliseconds
let mTZ = m + 18000000; //Asia/Tashkent +5 time zone. 18.000.000 = 5 hours in milliseconds.
let currentDate = new Date(mTZ);
I ran into this recently and had to do the same thing:
Read from excel as UTC
then convert into the timezone I wanted for data
I know from your question you don't want to do this, so perhaps this plan is something to fall back on.
My code was something like this:
const moment = require('moment-timezone')
let utc = moment.utc(excelCellStr, 'M/D/YY hh:mm A')
let t = moment().tz('America/New_York')
t.year(utc.year())
t.month(utc.month())
t.date(utc.date())
t.hour(utc.hour())
t.minute(utc.minute())
t.second(utc.second())
excelCellStr = t.format()
I am not sure if there is a setting to avoid this or not. We are using exceljs as well.
I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.
The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).
The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}
This is in an Ionic based hybrid app on NodeJS.
Trying to convert a local time as specified by a user input to another timezone, yet it fails:
static MTL_local_time_to_server(aDateTime:moment.Moment):moment.Moment{
console.log(aDateTime.format('MMMM Do YYYY, h:mm:ss a'));
const localTime:moment.Moment = momenttz.tz(aDateTime, momenttz.tz.guess());
console.log(localTime.format('MMMM Do YYYY, h:mm:ss a'), momenttz.tz.guess());
const returnTime:moment.Moment = momenttz(localTime).tz("Europe/Berlin");
console.log(returnTime.format('MMMM Do YYYY, h:mm:ss a'));
return returnTime;
}
Prints
April 22nd 2019, 12:00:00 am
April 22nd 2019, 12:00:00 am America/Los_Angeles
April 22nd 2019, 9:00:00 am
A few things:
You've typed the output of moment.tz as a string, but it's actually a Moment object.
When you call JSON.stringify on a Moment object, it returns the output of .toISOString(), which is always in UTC. (The Z indicates UTC in the ISO 8601 format.)
It's not clear if your input is a string or a Date object. If it's a string, then the Z indicates UTC, so it will always be interpreted as UTC. If it's a Date object, then the point in time represented by that Date object will be used - which depends on how you constructed the Date object. Either way, the value you're showing is UTC based, not local time.
It's not clear exactly what you're trying to accomplish. From your variable names, it would seem like you are trying to convert localDateTime to localTime, which logically would give the same value if they were both "local".
If you're trying to convert a value from local time to Berlin time, then:
moment(yourInput).tz('Europe/Berlin')
If you're trying to convert a value from Los Angeles time to Berlin time, then:
moment.tz(yourInput, 'America/Los_Angeles').tz('Europe/Berlin')
If you're trying to convert a value from UTC to local time, you don't need moment-timezone at all:
moment.utc(yourInput).local()
If you need string outputs, then you should call the format function to produce a string. What you showed here looks like you are logging Moment objects not strings.
I am trying to copy values across 2 datetime fields. While copying I want to set the time to 9:00 PM and pass the date value as it is.
Can anyone help on how to do this
I'm not sure I understand your question, but if you want to set the time component of a java.util.Date to 9PM, this should do it
Date date = new Date()
date.clearTime()
date.set((Calendar.HOUR_OF_DAY): 21)
However, this modifies the source Date object in-place. To avoid this, use the following instead:
Date date = new Date()
Date dateAt9PM = new Date(date.getTime()).clearTime()
dateAt9PM.set((Calendar.HOUR_OF_DAY): 21)
You can also use some groovy magic (C)
Date orig = new Date() + 10
Datew newDate = orig.updated( hourOfDay:19, minute:42, second:33 )
gives
Sat Mar 23 19:42:33 UTC 2019
I am trying to implement sending a 304 header for performance in a server hosting program I am writing, but I do not know how to parse the date of the If-Modified-Since header. I also would like to know how to find out if the If-Modified-Since date is older/newer than another date that I have in my code.
Just in case if someone comes across...
To parse date from "Last-Modified" you can use Date constructor that takes a date string.
You can also use Date.parse, which returns number of milliseconds since epoch (for invalid dates it returns NaN).
To print back date in format suitable for "Last-Modified" or "If-Modified-Since" header you can use Date's toUTCString() method.
var date = new Date("Wed, 17 May 2017 04:44:36 GMT");
var ms = Date.parse("Wed, 17 May 2017 04:44:36 GMT");
console.log('parsed date: ', date);
console.log('parsed date ms: ', ms);
console.log('If-Modified-Since: '+date.toUTCString());
To parse the date, use new Date(datestring) or Date.parse(datestring). To see if a date is newer or older than another date, use the greater than (>) and less than (<) operators.