I am using such code moment("24:33", "HH:mm").utc().format('HH:mm') and it returns Invalid date but should return "19:33" since there is 5 hour discrepancy between UTC and my local time. Why?
moment accepts only values from 00:00 to 23:59.
Related
I'm trying to get a date in milliseconds with moment.js. The problem is, in their docs in unix timestamp section they have only unix timestampt to momentjs format. How can I get a unix timestamp in milliseconds from date?
Eg: moment.unix(2010-01-01T05:06:07) => 1262318767000
You can get the date in milliseconds also with format().
moment("2010-01-01T05:06:07").format('x');
You said that "in their docs in unix timestamp section they have only unix timestampt to momentjs format", but that's not true.
According to https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/:
moment#valueOf simply outputs the number of milliseconds since the Unix Epoch
So, use the valueOf method:
moment("2010-01-01T05:06:07").valueOf();
But in my machine it returns 1262329567000. That's because moment.js is using my browser's timezone - this milliseconds value corresponds to January 1st 2010, at 05:06:07 AM in my browser's timezone.
The value you mention in your question (1262318767000) corresponds to January 1st 2010, at 05:06:07 AM in a timezone where the offset +01:00 is used: in some place that is one hour ahead of UTC, in January 1st 2010: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC+01:00,_A
If you want to be specific about what timezone the date/time corresponds to, you can use moment timezone: https://momentjs.com/timezone/
valueOf function https://momentjs.com/docs/#/parsing/utc/
console.log(moment('12/09/2021').valueOf());
In above example, you will not if it is 12 September or 09 January. It is better to provide the format, which format you're sending the value.
Checkout the below example,
moment(dateValue, dateFormat).valueOf();
So here we provide the date format which we are sending to moment and again converting it to a format moment understands and then taking out the milliseconds from 1 January 1970. If your date is less than that date, the value will be negative
Example:
console.log(moment('09/12/2021', 'DD/MM/YYYY').valueOf());
You can use
moment( date ).toDate().getTime();
And if you like to use timezone
let timezone = moment.tz.guess() || 'America/Los_Angeles';
moment( date, timezone ).toDate().getTime();
Can't format the below date using moment.js, the below statement returns Invalid Date
moment('20171206T062406927Z').format('D-MMM-YYYY');
Please help me on this.
You need to tell moment which format your date string is in:
moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');
Edit: updated as per #VincenzoC comment to ensure the timestamp is parsed in UTC
Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.
I'm trying to figure out the cleanest way to do a comparison in Teradata SQL Assistant. I have the scheduled start date (TimeStamp), the Schedule start time (varchar), actual start and end times (TimeStamp). I need to consolidate the scheduled start date and time and be able to compare it to the actual start and end date and time without modifying the original data (because it's not mine). I realize that the Scheduled Start Time [SST] is in a 24 hour time format with a AM/PM suffix, but like I said before, I can't change that.
I tried to do select cast(substr(scheduled_start_date,1,5) as TIMESTAMP(0)) from DB.TBL but am getting the "Invalid timestamp" error. There is example table data below.
Sch Start Date Sch Start Time Actual Start Actual End
09/11/2017 00:00:00 11:30 AM 09/11/2017 11:34:16 09/11/2017 11:58:00
05/26/2017 00:00:00 15:30 PM 05/26/2017 15:40:00 05/26/2017 15:55:15
11/06/2017 00:00:00 19:30 PM 11/06/2017 21:25:00 11/06/2017 21:45:00
Thanks!
You need to cast the schedule start time as an Interval, then you can easily add it to the start date:
scheduled_start_date
+ Cast(Substr(scheduled_start_time, 1,5) AS INTERVAL HOUR TO MINUTE)
A start date which is a timestamp seems to indicate this was ported from Oracle/SQL Server?
And a 24 hour time format with a AM/PM suffix is also quite strange.
A couple things to try:
Convert the separate Scheduled Date and Scheduled Time fields into strings, concatenate them, and feed that into a TIMESTAMP CAST. Something like:
SELECT
CAST(CAST(Scheduled_Date AS DATE) AS VARCHAR(25)) AS Date_String,
CAST(CAST(Scheduled_Time AS TIME FORMAT 'HH:MM BB') AS VARCHAR(25)) AS Time_String,
CAST(TRIM(Date_String) || ' ' || TRIM(Time_String) AS TIMESTAMP(0)) AS MyTimestamp
Cast the Scheduled Time field as a TIME data type. Cast the Scheduled Date field as a DATE data type. Then somehow combine the two into a TIMESTAMP field -- either with a CAST or some kind of timestamp constructor function (not sure if this is possible)
Option 1 should work for sure as long as you properly format the strings. Try to avoid using SUBSTRING and instead use FORMAT to cast as DATE/TIME fields. Not sure about Option 2. Take a look at these link for how to format DATE/TIME fields using the FORMAT clause:
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference%2FB035-1143-160K%2Fmuq1472241377538.html%23wwID0EPHKR
https://www.info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1143-160K/cmy1472241389785.html
Sorry, I don't have access to a TD system to test it out. Let me know if you have any luck.
I want to convert the given time into required time format in javascript using moment.js
moment('2014-10-05T22:25:00-07:00').format('DD-MM-YYYY HH:ss Z');
I am tried the above code to get result "06-10-2014 3.30" but it returns "06-10-2014 10:00 +05:30"
The time you are passing in is
2014-10-05T22:25:00-07:00
which is 5:25am on the 6th of October, GMT. You're then outputting it using the timezone UTC+05:30, which should give:
06-10-2014 10:55:00 +05:30
Unfortunately, you've missed mm from your date string, so you are only seeing:
06-10-2014 10:00 +05:30
Simply change your date format to DD-MM-YYYY HH:mm:ss Z to correct this.
I parse this date "22/11/11" into a DateTime object called s. When I do s.getDayOfMonth() it gives me 22 which is right. However, when I convert the DateTime object to a Date object and try to get the date using s.toDate().getDate() it returns 24 which is not right. Does anyone have an idea why is this happening ?
The Joda-Time DateTime.toDate() method converts to a java.util.Date with the same millisecond instant. The java.util.Date.getDate() method uses the local default time-zone to return its value, whereas DateTime.getDayOfMonth() uses the time-zone stored in the DateTime object. If the two time-zones are different, you may see a difference, explaining the 22/24 you observe.
To understand further, print the time-zone of the DateTime, and the default Java zone TimeZone.getDefault() used by java.util.Date.