Extract epoch from moment object - javascript

I am trying to extract the unix timestamp (epoch) from a date string as follows:
var week = $("#week").val();
var timestamp = moment(week).format("X");
console.log(timestamp);
This returns, in the console "Invalid Date".
I am passing the following format: "03-Nov-16"
I am trying to return the unix timestamp of a date.
Any help on this would be appreciated.

You need to tell moment the format of the date string you're trying to parse. I think you're looking for something like this (not tested):
var timestamp = moment("03-Nov-16", "DD-MMM-YY").unix()
See the official docs on date string parsing for more info.

Related

How to convert an invalid date string to Javascript date

I fetch invalid date strings from the REST API but i may not fix the REST API. How can i format an invalid date strings like that "20180517T010237" ?
I tried to use moment for that, but i couldnt succeed.
let date = moment("20180517T010237", "YYYY-MM-DD T HH.mm.ss").toDate();
is there any easy way to do that?
The second string you pass moment is the format of the string you're parsing. Your format string has - and spaces that aren't in your input. Remove them:
let date = moment("20180517T010237", "YYYYMMDDTHHmmss").toDate();
Note that it will be parsed in local time. If you want UTC instead, use moment.utc:
let date = moment.utc("20180517T010237", "YYYYMMDDTHHmmss").toDate();
Example:
let date = moment("20180517T010237", "YYYYMMDDTHHmmss").toDate();
console.log(date);
date = moment.utc("20180517T010237", "YYYYMMDDTHHmmss").toDate();
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

How to convert date time string in formatted date time in extjs

I have a string of Date and Time ("2017-11-29 11:08:43" YYYY-MM-DD hh:mm:ss) like this. I want to convert it into "29-11 11:08"(DD-MM hh:mm) format.
I tried it using below code. But not get any success. have you any solution?
convert: function (idleFrom) {
var date = Ext.Date.parse(idleFrom, "Y-m-d");
return date;
}
first change your string into date format using
var dt = new Date(idleFrom)
than change into you required format using
Ext.Date.format(dt, 'm/d/Y');
follw this link for more format
Hope it will work :)
If you have a string, and want it reformatted, you have to parse the string into a JS date object first, and then format the JS date object into the string representation you need:
var date = Ext.Date.parse("2017-11-29 11:08:43", "Y-m-d H:i:s")
var str = Ext.Date.format(date, "m/d/Y")
Please note that Ext.Date.parse is really picky regarding the format identifier. If the matching between the format identifier and the input string's format is not 100%, your date will be null.
E.g. Ext.Date.parse("2017-11-29 11:08:43", "Y-m-d H:i") will be null because the seconds are in the date string, but missing from the format identifier.

Get date time in specific format in moment js

I am trying to get the date time in moment js in this format :
2016-12-19T09:43:45.672Z
The problem is I am able to get the time format as
2016-12-19T15:04:09+05:30
using
moment().format();
but I need the format as the first one [like .672Z instead of +05:30]. Any suggestions would be of great help.
From the documentation on the format method:
To escape characters in format strings, you can wrap the characters in square brackets.
Since "Z" is a format token for the timezone, you need to escape it. So the format string you are after is:
moment().format('YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
As #VincenzoC said in a comment, the toISOString() method would also give you the format you are after.
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment().utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment().toISOString();
Try this
const start_date = '2018-09-30';
const t = moment(start_date).utc().format();
console.log(t);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
var dateTime = new Date("2015-06-17 14:24:36");
dateTime = moment(dateTime).format("YYYY-MM-DD HH:mm:ss");
Try this. You should have the date in the correct format.

MomentJS change the format from YYYYMMDD

I have this format of date YY which looks like 20141229 I'm trying to get it into Timestamp format
I tried moment(date).format('x') but I get "Invalid date", not sure how to make this done, any help please ?
thanks!
moment constructor takes format as second parameter, so moment('20141229', 'YYYYMMDD') will give you valid moment object. and calling .unix() should give you timestamp.
Docs
Try separating segments with a space, for example
var date = "2014 12 29"
var stamp = moment(date).format("x")

Data conversion using Node JS

I'm using the module dataformat to get a data from a timestamp but I get an error and I can't understand why.
that's the timestamp 1405303200 and I need the Date in this format dd/mm/yyyy
That's my code
var day=dateFormat(result.request_date, "dd/mm/yyyy");
But what I get is the 1970-01-17 date asd.
I think you are using a timestamp saved by PHP... now convert it to miliseconds
var timestamp = 1405303200 *1000;

Categories

Resources