convert string to ISO date time in nodejs - node.js

Convert this date into ISO format in nodejs
created_at="September 17th 2019, 16:50:17.000";
let new_time = new Date(created_at);
created_at = new_time.toISOString();
console.log(created_at);
Output: Invalid Date
Exacting output is in ISO format. like this 2011-10-05T14:48:00.000Z

Moment.js is a library which you can use to get the output and do some advanced operations with date and timezones.
Below is the code to get expected output.
var moment = require('moment')
created_at="September 17th 2019, 16:50:17.000";
let new_time = moment("September 17th 2019, 16:50:17.000", "MMMM Do YYYY, HH:mm:ss:SSS");
created_at = new_time.toISOString();
console.log(created_at);

You will have to pass the date string in below format in order to convert it to ISO date:
var date1 = "September 17 2019, 16:50:17.000";
let new_date = new Date(date1);
console.log(new_date);
console.log(new_date.toISOString());

Related

NodeJs How to use moment-timezone to convert Epoch to Specific TImezone and format

I have a use-case in which I want to convert an Epoch integer value into a specific Timezone time and a specific format,
Also, I want to convert a human-readable date time into epoch.
I am trying to use moment-tz for the timezone conversion.
I using a specific Epoch timestamp 1555296000 which is -
Monday, April 15, 2019 10:40:00 AM in Kuala Lampur Malaysia,
I am able to convert 2019-04-15 10:40:00 of Asia/Kuala_Lumpur timezone into correct Unix.
But I am unable to convert 1555296000 into another timezone's unix,
i.e I wish to convert 1555296000 into equivalent YYYY-MM-DD hh:mm:ss of Asia/Calcutta timezone.
Following is the code I'm trying to work with -
var moment = require('moment-timezone');
console.log("Convert from Asia/Kuala_Lumpur to Unix -> ", moment.tz("2019-04-15 10:40:00","Asia/Kuala_Lumpur").unix());
// Outputs - 1555296000
console.log("Epoch to Specific TimeZone and Format -> ",moment(1555296000).tz("Asia/Calcutta").format('YYYY-MM-DD hh:mm:ss'));
// Outputs - 1970-01-19 05:31:36
// I want - 2019-04-15 08:10:00
Try this out
const moment = require("moment-timezone");
console.log(
moment
.unix(1555296000)
.tz("Asia/Calcutta")
.format("YYYY-MM-DD HH:mm:ss")
);
2019-04-15 08:10:00 - "Asia/Calcutta" is 2019-04-15 10:40:00 - "Asia/Kuala_Lumpur"

How to change a given date in "yyyy-MM-dd HH:mm:ss.SSS" format to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" format in groovy

How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Format the time from moment js

When I am trying to receive mail from gmail, I get time in this format (Mon, 12 Jun 2017 10:29:07 +0530). I want to calculate time minus current time. How to do so?
var testDate = moment("Mon, 12 Jun 2017 10:29:07 +0530");
//Relative to time in human readble format
testDate.fromNow(); //3 days ago
You can simply call the moment constructor with the given Date format. moment.js is smart enough to parse it for you. To get the difference you can convert it into unix based time format and subtract it.
const givenTime = moment("Mon, 12 Jun 2017 10:29:07 +0530").unix()
const currentTime = moment().unix()
//Difference in milliseconds
const diff = givenTime - currentTime

Convert yyyymmddThhmmss.SSSZ format to Unix Timestmp

I'm using NodeJS to fetch a date from a server, but the date format is yyyymmddThhmmss.SSSZ
20170423T203146.000Z
I'm trying to convert this date string into epoch time, to easily calculate a difference between this time and the current time. (the timezone will always be UTC)
However, I could not find any possibility to parse this string, since libraries don't seem to accept this kind of date string.
Can someone help me out with this?
Moment.js seems to give me correct parsed date
var moment = require('moment')
var date = moment("20170423T203146.052Z" , "YYYYMMDDThhmmss.SSS")
console.log(date.format("YYYY MM DD hh mm ss SSS"))
Output: 2017 04 23 08 31 46 052
you should take a look at Moment.js String+Format + Special Format
It seems to work very well on your time-string already:
const moment = require("moment");
const werner = "20170423T203146.000Z";
console.log(moment.utc(werner).format());
const epochSec = moment.utc(werner).unix();
const epochMs = moment.utc(werner).valueOf();
console.log(epochSec, epochMs);
Viel Erfolg ;)

momentjs wrong date returned

Im using momentjs in nodejs app and trying to parse timestamp string with momentjs to compare two dates. My code is:
moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss')
and the output is:
{ _date: Fri Nov 04 2016 00:00:00 GMT+0000 (GMT Standard Time) }
But with the given format im expecting the return date to be Sun Apr 10 2016.
Seems like the format option is not working or im missing something
It works for me (moment version: 2.12.0)
var moment = require('moment')
var m = moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss')
console.log(m.format('LLL')) // April 11, 2016 1:00 AM
Also changing days with months in both arguments works:
var m = moment('04/11/2016 01:00:00', 'MM/DD/YYYY HH:mm:ss') // same date as above
What version of moment are you using and how did you import it? How did you generate the output date? Is it just console.log?
(this should be a comment, but I wanted to add the code snippet)
Here's a sample code to return a string in your output format
var moment = require('moment');
var now = moment();
var date = moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss');
console.log(now.format('ddd MMM D YYYY'));
console.log(date.format('ddd MMM D YYYY'));
Sorry it was my mistake. I've installed momentjs which is not the official moment package. After installing the moment package instead momentjs all is working as expected.

Resources