How to convert the following time format (2014-10-05T22:25:00-07:00) into required date format in javascript - momentjs

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.

Related

Reactive mat-datepicker formatting using luxon

I'm trying to replace momentjs with luxonjs which is used in mat-datepicker of a reactive form, initially I'm loading the datepicker field with ISO format
'2022-05-10T10:34:31.311-04:00'
for which I used below luxon formatting to save the values
DateTime.fromISO(
this.form?.get('date')?.value
).toISODate();
DateTime.fromISO(this.form?.get('date')?.value)
.plus({ day: 1 })
.toISODate();
When I select different date, the date format is different from my original
Sun May 01 2022 00:00:00 GMT-0400 (Eastern Daylight Time)
How to change this format to ISO using luxon
I solved this issue with a workaround by converting the reactive form date value to ISO format using angular DatePipe
let date = new DatePipe('en-US').transform(
this.form?.get('date')?.value,
'yyyy-MM-ddThh:mm:ss'
);
date = DateTime.fromISO(date).plus({ day: 1 }).toISODate();

ISO 8601 Datetime understanding

I am trying to understanding the date reading of ISO 8601 format and cant find a good doc to read.
If I am getting time in my code like this "2018-08-18T00:00:00+1000", is this local time or UTC time?
and when I convert it using Convert.ToDateTime("2018-08-18T00:00:00+1000"), I get the following date time {8/17/2018 7:00:00 AM}. I am not sure if that is UTC datetime or local?
What is the math behind "2018-08-18T00:00:00+1000" getting converted to {8/17/2018 7:00:00 AM}, I cant get my head around this.
You are asking for the math behind 2018-08-18T00:00:00+1000 being shown as 8/17/2018 7:00:00 AM.
First of all 8/17/2018 7:00:00 AM is just another format to display the date and time. Converted to an ISO 8601 string it would look like this: 2018-08-17T07:00:00.
+1000 at the end of the 2018-08-18T00:00:00+1000 representation is a timezone offset. You could read that string as August 18, 2018 in UTC plus ten hours. So it would be the same as 2018-08-18T10:00:00Z.
So we have a UTC date of August 18, 2018 10 AM, which is shown as a locale date of August 17, 2018 7 AM. That would mean that you are in a timezone which is 27 hours behind UTC.
A timezone behing more than 12 hours before (or 14 after) UTC does not exist (as far as I'm aware of). Therefore I assume that you have a typo in your example. Another reason could be a totally broken date parser.
But I still hope you got the math behind the conversion.

TeradataSQL: Time to String, Add to Date and Compare to Another Time and Data

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.

Moment not correctly parsing time in milliseconds

I have the following code:
job_created_at_timestamp = moment(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:MM:SS A');
Where job_created_at_timestamp_milliseconds is a timestamp in milliseconds, for example: 1463422849081.
Moment is not correctly converting these timestamps, for example 1463422849081 is converted to 2016-05-16 02:05:08 PM, when it should be 2016-05-16 02:20:49 PM.
I also tried to 'reconvert' the timestamp into a unix time:
moment.unix(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:MM:SS A');
but that timestamp is less correct.
I verified that the milliseconds are correct using this website. Ideas?
The timestamp format was wrong:
moment(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:mm:ss A');

How to convert time to seconds with SQLite

I want to be able to store for example 2:30pm in a column in SQLite, and be able to pull that time and convert it to seconds, does SQLite have a function for this?
I was reading http://www.sqlite.org/datatype3.html and they do state they have a time function, but can I do this? I know that SQLite does not have a time datatype, but what type should I store it as then, varchar?
2:30pm --> 52200
I was reading more into this: http://www.sqlite.org/lang_datefunc.html
and it seems like the list of time acceptable is :
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
So does that mean you can't use 2:30pm as a format?
Since you will need the value to be in seconds from the beginning of the day (per your clarification) you can just store as an integer. To convert it to seconds from the beginning of the day rather than 1970 simply subtract midnight of the current day from it.
Something like this
strftime('%s','2004-01-01 14:30:00') - strftime('%s','2004-01-01 00:00:00')
If 2:30pm that you are trying to store is current time then you can shorten it to
strftime('%s','now') - strftime('%s','2004-01-01 00:00:00')

Resources