I had the same issue. I get a date as a String, for example: '2016-08-25T00:00:00', but I need to have the Date object with correct time. To convert String into object, I use getTimezoneOffset:
var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() + userTimezoneOffset);
getTimezoneOffset() will return ether negative or positive value. This must be subtracted to work in every location in world.
I had the same issue. I get a date as a String, for example: '2016-08-25T00:00:00', but I need to have the Date object with correct time. To convert String into object, I use getTimezoneOffset:
var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() + userTimezoneOffset);
getTimezoneOffset() will return ether negative or positive value. This must be subtracted to work in every location in world.
The date is parsed correctly, it's just toString that displays the timestamp in your local timezone:
let s = "2005-07-08T11:22:33+0000";
let d = new Date(Date.parse(s));
// this logs for me
// "Fri Jul 08 2005 13:22:33 GMT+0200 (Central European Summer Time)"
// and something else for you
console.log(d.toString())
// this logs
// Fri, 08 Jul 2005 11:22:33 GMT
// for everyone
console.log(d.toUTCString())
Javascript Date object are time values - they merely contain a number of milliseconds since the epoch. There is no timezone info in a Date object. Which calendar date (day, minutes, seconds) this timestamp represents is a matter of the interpretation (one of to...String methods).
The above example shows that the date is being parsed correctly for offset +0000 - that is, it actually contains an amount of milliseconds corresponding to "2005-07-08T11:22:33" in GMT.
You can use the DateTime property of DateTimeOffset.
Example:
string s = "2013-07-22T08:51:38.000-07:00";
var dateTimeOffset =DateTimeOffset.Parse(s, null);
Console.WriteLine(dateTimeOffset.DateTime);
Outputs:
22/07/2013 08:51:38
you can try this.
DateTimeOffset.Parse("2013-07-22T08:51:38.000-07:00").DateTime.ToString("dd-MM-yyyy hh:mm:ss tt");
Hello,
Is it possible to get the difference between 2 datetime if I dont have reference of the timezone?
From the API I get the datetime like 2023-11-14T08:30:00 for departure and 2023-11-14T12:45:00 for arrival.
By looking at it, the difference will be 4hr and 15min. But the flight is from Dubai to Los Angeles which should be 16hr and 15min.
I will have occations that I will get timezone will be + or -.
As far as I understood your requirement; you want to convert the timestamp to a date string without timezone, for which you can use the toISOString() method and then remove the timezone information from the resulting string.
const timestamp = 1701699255;
const date = new Date(timestamp * 1000);
const dateString = date.toISOString().replace(/T/, ' ').replace(/\..+/, '');
console.log(dateString); // "YYYY-MM-DD HH:mm:ss" without timezone information.
Another approach is to use getUTC* methods in case you want to keep the Date object and display it without timezone.
const timestamp = 1701699255;
const date = new Date(timestamp * 1000);
const year = date.getUTCFullYear();
const month = (date.getUTCMonth() + 1).toString().padStart(2, '0');
const day = date.getUTCDate().toString().padStart(2, '0');
const hours = date.getUTCHours().toString().padStart(2, '0');
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
const seconds = date.getUTCSeconds().toString().padStart(2, '0');
const dateString = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(dateString);
const str = new Date().toLocaleString('en-US', { timeZone: 'Asia/Jakarta' }); console.log(str);
You can set a time zone for the duration of the query thusly:
TZ=America/New_York date
Note the whitespace between the TZ setting and the date command. In Bourne-like and rc-like shells, that sets the TZ variable for the current command only. In other shells (csh, tcsh, fish), you can always use the env command instead:
env TZ=America/New_York date
tl;dr
On Linux systems. time zones are defined in files in the /usr/share/zoneinfo directory. This structure is often referred to as the "Olson database" to honor its founding contributor.
The rules for each time zone are defined as text file lines which are then compiled into a binary file. The lines so compiled, define the zone name; a range of data and time during which the zone applies; an offset from UTC for the standard time; and the notation for defining how transition to-and-from daylight saving time occurs, if applicable.
For example, the directory "America" contains the requisite information for New York in the file America/New_York as used, above.
Beware that the specification of a non-existent zone (file name) is silently ignored and UTC times are reported. For example, this reports an incorrect time:
TZ="America/New York" date ### WRONG ###
The Single UNIX Specification, version-3, known as SUSv3 or POSIX-2001, notes that for portability, the character string that identifies the time zone description should begin with a colon character. Thus, we can also write:
TZ=":America/New_York" date
TZ=":America/Los_Angeles" date
As an alternative method to the specification of time zones using a path to a description file, SUSv3 describes the POSIX model. In this format, a string is defined as:
std offset [dst[offset][,start-date[/time],end-date[/time]]]
where std is the standard component name and dst is the daylight saving one. Each name consists of three or more characters. The offset is positive for time zones west of the prime meridian and negative for those east of the meridian. The offset is added to the local time to obtain UTC (formerly known as GMT). The start and end time fields indicate when the standard/daylight transitions occur.
For example, in the Eastern United States, standard time is 5 hours earlier than UTC, and we can specify EST5EDT in lieu of America/New_York. These alternatives are not always recognized, however, especially for zones outside of the United States and are best avoided.
HP-UX (an SUSv3 compliant UNIX) uses textual rules in /usr/lib/tztab and the POSIX names like EST5EDT, CST6CDT, MST7MDT, PST8PDT. The file includes all of the historical rules for each time zone, akin to the Olson database.
NOTE: You should be able to find all of the time zones by inspecting the following directory: /usr/share/zoneinfo.
You can do this by manipulating the TZ environment variable. The following will give you the local time for US/Eastern, which will also be smart enough to handle DST when that rolls around:
# all on one line
TZ=":US/Eastern" date +%Y%m%d
The zone name comes from the files and directories inside /usr/share/zoneinfo.