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")
Related
for a weight tracking project i want to get a date from a user in his specific format eg:"05-12-19" , and i want to format it with momentjs to the standard javascript format
This code below is what i tried and think is the nearest to the result i want:
let newDate = moment().format("05-12-19","DD-MM-YYYY");
console.log(newDate); //05-12-19
the result that i was expecting is 05-12-2019 but got something different take a look here (trying to meet stack-overflows quality standards lol)
To create your date, something like this:
let newDate = moment("05-12-19","DD-MM-YY");
console.log(newDate.toDate());
to output your desired format
let newDateStr = moment("05-12-19","DD-MM-YY").format("DD-MM-YYYY");
console.log(newDateStr);
after looking for a while i found a similair answer here.
it wasn't exactly what i was looking for so i'll post here my complete answer:
let newDate = moment("05-12-19", "DD-MM-YY").format("DD-MM-YYYY");
console.log(newDate);
in the moment function the first argument is my date, the second argument is the format of this date, because momentjs doesn't know this format. in the format function i enter the date i want it to format to.
I am using jquery.ui.monthpicker library. For month picker I am getting date like 07/2017. From this date string I need to calculate previous month and formatted like 1707 using moment js library.
any help would be appreciated.
This code may solve your problem.
moment("07/2017", "MM/YYYY").subtract(1, 'months').format('YYMM');
DEMO at https://jsfiddle.net/nffswx75/
var dt = "07/2017";
alert(moment(dt,"MM/YYYYY").format('YYMM'));
alert(moment(dt,"MM/YYYYY").add(-1, 'months').format('YYMM'));
alert(moment(dt,"MM/YYYYY").subtract(1, 'months').format('YYMM'));
You can let moment create a date object from a string by telling it what format your date is in.
let dateString: string = "07/2017";
var date = moment(dateString, "MM/YYYY").subtract(1, 'month').format("YYMM");
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.
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.
I have the following HTML:
<strong id="ut-open-date">27/06/2014</strong>
and I want to read the text/innerhtml and convert its format to "YYYY-MM-DD" so I can insert it into MySQL table. I am using the moment.js library and my code is below:
var CreateDate = moment(jQuery('#ut-open-date').html()).format("DD/MM/YYYY");
CreateDate = moment(CreateDate).format("YYYY-MM-DD");
But the code changes 27/06/2014 to 2016-06-03 and I cannot work out why.
I also tried this code with the same result.
var CreateDate = moment(jQuery('#ut-open-date').html()).format("YYYY-MM-DD");
Any help is appreciated.
If we break down your code step by step you can see where it is going wrong:
var CreateDate = moment(jQuery('#ut-open-date').html())
This part uses the default constructor to try to parse the date, this is unreliable at best and has been deprecated. So moment is trying to guess what the date format is here.
.format("DD/MM/YYYY");
This is taking what ever was read in step 1 and trying to turn it into a string with the format of DD/MM/YYYY
CreateDate = moment(CreateDate)
Now you are parsing again without specifying the format so moment is doing it's best to guess
.format("YYYY-MM-DD");
Now you have told it to turn whatever it guessed the date to be into a string with the format YYYY-MM-DD
Do this instead:
var CreateDate = moment(jQuery('#ut-open-date').html(), 'DD/MM/YYYY').format('YYYY-MM-DD');
The moment(dateString) form is deprecated you should use the form moment(dateString, expectedFormat) instead.
See moment documentation here: http://momentjs.com/docs/#/parsing/string-format/
Thanks to athms for link
The problem is you need to tell moment.js what format for date string you want parse by specifying second parameter. See all the supported format. If your format is not listed (See the Open Issue for NON-ISO strings), you need to specify the date format parameter.
moment( jQuery('#ut-open-date').html(), "DD/MM/YYYY" )
DEMO
I solved it by using split as follows:
var CreateDate = jQuery('#ut-open-date').text();
var DateArray = CreateDate.split('/');
CreateDate = DateArray[2] + '-' + DateArray[1] + '-' + DateArray[0];