how do I maintain time offsets in moment? - node.js

I'm trying to add/subtract days from moment time object. However, the problem im having is that the time offset is different then the local time of the server.
in db my time
2019-08-14T21:38:50-04:00
however, locally I get +0000
moment().format('YYYY-MM-DD hh:mm ZZ')
2019-08-15 07:19 +0000
so now if I try to convert the time stored in the db
moment("2019-08-14T21:38:50-04:00").format('YYYY-MM-DD hh:mm ZZ')
2019-08-15 01:38 +0000
if I use parseZone() I get
moment.parseZone("2019-08-14T21:38:50-04:00").format('YYYY-MM-DD hh:mm ZZ')
2019-08-14 09:38 -0400
I also tried manually removing the offset of 4 hours (to balance it out?)
moment("2019-08-14T21:38:50-04:00").utcOffset(-240).format('YYYY-MM-DD hh:mm ZZ')
2019-08-14 09:38 -0400
But the value out (after formatting) is always different then in.
How do I get 2019-08-14T21:38:50-04:00 as the output after I format moment obj back to a string?

moment.parseZone("2019-08-14T21:38:50-04:00").format()
or
moment.parseZone("2019-08-14T21:38:50-04:00").format(moment.ISO_8601())
or
moment.parseZone("2019-08-14T21:38:50-04:00").format("YYYY-MM-DD[T]HH:mm:ssZ")
All of the above will produce "2019-08-14T21:38:50-04:00", the same as the input string.
The main problem in your third attempt is that you were using hh (12-hour clock) instead of HH (24-hour clock).

Related

Excel - How to convert Day of Week - Mon - Day - UTC - Year to recognizable date in excel

I'm working on a file that pulls data directly from a web source, but I need to calculate time elapsed since a date. The problem is this is the format the date is appearing in:
Sun May 08 00:00:00 UTC 2016
How to I convert this automatically to MM/DD/YYYY when I refresh the data so that it appears as:
05/08/2016
use:
=--(MID(A1,9,2)&MID(A1,4,5)&RIGHT(A1,4))
and format the output however you want:

Convert Date/Time Text String to Time Format

I am running a report on a website that spits this out in a CSV format:
Thu May 07 2020 12:16:08 GMT+0000 (Coordinated Universal Time)
I want to convert it to a time format in Excel, ideally MM:SS (no hours needed). I need to be able to add/subtract two times.
Tried various formulas, but the string is so long I keep confusing myself. Can someone assist?
Consider:
=TIMEVALUE(MID(A1,FIND(":",A1)-2,8))
with the proper formatting.

Excel Date/Time formatting AM vs PM error

I am using Windward to generate a report with readable dates. Winward uses Excel date/time formatting for formatting, however I am experiencing an issue when formatting a date/time within the 12th hour (eg 12:30) showing as AM rather than PM.
Are my Excel input/output date/time format strings incorrect?
Input format
yyyy-MM-dd hh:mm:ss.S
Output format
dd MMM yyyy h:mm AM/PM
Examples
2016-11-24 00:15:00.0 evaluates to 24 Nov 2016 12:15 AM OK
2016-11-25 12:15:00.0 evaluates to 25 Nov 2016 12:15 AM WRONG
2016-11-26 13:15:00.0 evaluates to 26 Nov 2016 1:15 PM OK
The issue isn't to do with the Excel date/time format strings. Windward uses java's SimpleDateFormat class to evaluate date inputs and Excel Date Formatting to output the formatted date. The following input format works correctly.
yyyy-MM-dd HH:mm:ss.S
As per oracle documentation here.
Letter Date or Time Component Presentation Examples
H Hour in day (0-23) Number 0
h Hour in am/pm (1-12) Number 12

Time changes when converting date into ISOString in nodejs

I've got a date string from my database which has this format:
Tue Nov 12 2013 18:14:46 GMT+0100 (CET)
I want to convert it into a ISOString and im currently doing that with:
var iso = new Date(val.text_date).toISOString();
However for some reason the output time is moved 1 hour backwards?
This is the output im getting:
2013-11-12T17:14:46.000Z
How can i avoid this?
Short answer: the time is converted into UTC, and your original time was displayed in UTC+1, hence the one hour difference.
The Date.toISOString() method converts the date into a string in the ISO 8601 format. Note that the returned date in your example ends by a Z: 2013-11-12T17:14:46.000Z. As per the Mozilla documentation and Wikipedia:
If the time is in UTC, add a Z directly after the time without a
space. Z is the zone designator for the zero UTC offset

Removing time element of Long Date Format

I have a list of data within the last three years, but all the dates have different times on it. I am trying to write a macro that will delete all the times. I tried changing the format of the cell but it didn't work.
EX. the list looks like:
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
10/12/2011 08:41 PM Eastern Time
And I need to delete the 08:41 PM Eastern Time or equivalent from each. All the times are different though. Any thoughts?
If actually date format, try =INT(A1) then format to suit.
This should cover either text or time format:
=IF(CELL("type",A1)="l",LEFT(A1,FIND(" ",A1)),INT(A1))
or even:
=TEXT(IF(CELL("type",A1)="l",LEFT(A1,FIND(" ",A1)),INT(A1)),"mm/dd/yyyy")
if the output is to be formatted mm/dd/yyyy.
If the field is an excel date and not plain text, you can use the trunc function. It should trim the time portion off.
=TRUNC(A1)
Why not simply:
=Left(A1,10)
Assuming all dates look the same (mm/dd/yyyy) format
Just in case it isn't always the first ten characters (examples: 1/1/2011 or 3/14/12), you can use this formula. Be sure to format the cell as date:
=--LEFT(A1,FIND(" ",A1)-1)
Not sure why this is not mentioned in any of the answers as its by far the fastest and easiest way to do this. You can use the DATEVALUE to convert the dates from text to an excel date if they are not already in an excel date format.
Once the date is in a number format (eg 42753.597534 which excel recognizes as January 18, 2017 2:20:27 PM): Excel equates dates to numbers and 1 is equivalent to 24 hours(1 day), while anything less than 1 is used for time. You can use ROUNDDOWN(A1,0) which will strip any non-integer part of the date which implicitly gets rid of the time (turning the above number into 42753 or January 18 , 2017.

Resources