i have a date in string like this: var myDateStr='1431451872338.00';
i want, getMonth() from this format date, i do:var date = new Date(myDateStr); but always return invalid date.
and the method getMont() always return NaN, if I put this: var date = new Date(1431451872338.00); this return the date correct but with my string not
my var myDateStr get the value from json and is variable, if someone can help me thank you very much in advance, i hope do understand
This works fine for me. You just need to be sure you're inputing a number, not a string.
var number = parseInt("1431451872338.00");
var date = new Date(number); //Tue May 12 2015 12:31:12 GMT-0500 (CDT)
var month = date.getMonth(); // 4
A Date object cannot be instantiated with a string. You better 1st transform your string into an Int and then ask for month:
var myDateStr='1431451872338.00';
var date = new Date(parseInt(myDateStr, 10));
alert(date.getMonth());
Could you use parseInt and do something like this:
var myDateStr = '1431451872338.00';
var myDateInt = parseInt(myDateStr, 10);
var myDate = new Date(myDateInt);
When you're passing in a date string to the javascript date object, it needs to be in the format "yyyy/mm/dd" or something like "January 10, 2014". What you're passing is the number of milliseconds since January 1, 1970, which is only accepted by the date object as a number. You need to change the type of your input variable.
Please make sure to research carefully before answering questions - the answer to your question is clearly stated on many date references like this one.
One Liner
var month = date.getMonth(Date(parseInt("1431451872338.00")));
Related
I'm working with Javascript within Google Sheets, and I'm having trouble converting or parsing a formatted timestamp, to ultimately extract the day as a numerical value.
My code:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
Logger.log(date.getDay());
The output:
[19-06-10 17:40:56:107 BST] NaN
My goal is to extract the day number, for example, "18" from that timestamp.
However, it doesn't seem to convert it. I suspect my timestamp isn't in the correct format for the date() function, so it's about creating a function to parse it.
Hopefully, you can help me with that! :) Thank you so much.
The date object has a method like this for getting the day of the month as a number (1-31).
date.getDate();
18 is date.
var shopifyTimestamp ="2019-05-18 13:21:17 +0100";
var date = new Date(shopifyTimestamp);
console.log(date.getDate());
JavaScript's Date constructor supports ISO 8601 date strings. Without using any libraries, you can do something like this:
var shopifyTimestamp = "2019-05-18 13:21:17 +0100";
// will produce `2019-05-18T13:21:17+0100`
var isoDate = shopifyTimestamp.slice(0, 10)
+ 'T' + shopifyTimestamp.slice(11, 19)
+ shopifyTimestamp.slice(20);
var date = new Date(isoDate);
console.log(date.getDate()); // 18
Also note that you're looking for date.getDate(), rather than date.getDay(). The latter returns the numerical date of the week.
I am running into a problem with when I try to pass a Date() variable I have created to a function that I working on, if I put the following in the function I have passed the variable to this is what I see.
console.log("quickScreenCompletedDate: " + typeof quickScreenCompletedDate);
console.log("quickScreenCompletedDate: " + quickScreenCompletedDate);
here is the output
"quickScreenCompletedDate: number"
"quickScreenCompletedDate: 1403409600000"
However when I create the variable I am creating it as a Date() class
var completedDate = Date.parse('#Model.CompletedDate');
then I call the function like this.
previousDenialDate_ChangeHandler(isChild, completedDate);
for completeness here is the function definition
function checkIfShouldShowPreviouslyDeniedMessage(isChild, quickScreenCompletedDate) {
The problem is that i need to do some logic on the compeletedDate var and that works just fine as you can see I am getting the number 1403409600000, now though, I also need to display this to the user and when I try to call toDateString() that fails as it isn't a date object anymore. What am I missing that is causing this.
Thanks
From the documentation for Date.parse()
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
So:
var completedDate = Date.parse('2014-07-01');
returns 1404172800000 (of type number)
Instead, do:
var completedDate = new Date('#Model.CompletedDate');
which will give you an actual Date object instance.
Javascript stores dates as milliseconds since Jan 1 1970.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You do the math in milliseconds and then convert to a date using toDateString() to print it as a date OR you can create a new Date and pass in the milliseconds to the constructor.
Example
new Date(1403409600000);
why don't you try with new Date?
new Date(year, month, day [, hour, minute, second, millisecond ])
in your case try
var completedDate = new Date('#Model.CompletedDate.Year','#Model.CompletedDate.Month','#Model.CompletedDate.Day');
or
var completedDate = new Date('#YourModelVariable');
If you use Date.parse Beware of timezone issues
var d = Date.parse("10/22/2014");
refer here for documentation of Date.parse
I have a system that returns a JSON object that contains dates in string format.
These dates are in the format "2012-10-19 06:05:38 GMT" (no... I'm stuck with them like this)
So I need to get this into a date object (d) ready to output as d.toLocaleDateString()
In chrome it works perfectly by just passing the string to a new Date (Bad bad Chrome - makes Eric lazy), but of course it fails in FF and IE
I can fix it by splitting the string but its not pretty and I've not figured out dealing with the offsets from GMT.
There must be a more elegant way...?
I'm sure someone here can do it in one line.
It's not quite a one-liner, but if you know all your dates will be GMT, something like the following should work:
function parseDate(dateString) {
// [y, m, d, hr, min, sec]
var parts = dateString.match(/\d+/g);
// Months are 0-indexed
parts[1] -= 1;
return new Date(Date.UTC.apply(Date, parts));
}
If I were you, and had access to the serverside script gathering that information (and outputting it) I would convert the date into a unix timestamp, and then make Javascript process that using the Date constructor easily.
EDIT: You can use strtotime() function to convert the string date into numeric unix timestamp if you're using PHP.
If you know the exact format, you could use a library such as Moment.js: Documentation for Moment.js.
To parse:
var dateString = "2012-10-19 06:05:38 GMT".replace(" GMT", "");
var date = moment(dateString, "YYYY-MM-DD HH:mm:ss");
You can just parse the dateString manually,and pass the Date the Date constructor exactly:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
var dateString = "2012-10-19 06:05:38 GMT".split(" "),
date = dateString[0].split("-"),
time = dateString[1].split(":");
var dateObj = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
My value contains "08.07.1987", how to retrieve the date object for this string. new Date(val) gives correct date object values only for the string value that contains "/" format. can any one let me know hot to create date object for the values which contains "." or "-". in its format.
How about just adjusting the string to suit your needs?
var date1 = new Date("08.07.1987".replace('.','/'));
var date2 = new Date("08-07-1987".replace('-','/'));
You will need to be careful when asking Javascript to interpret a date in this format. As you can probably imagine, a date listed as "08.07.1987" doesn't really specify whether it's August 7th or July 8th.
In general, your best bet will be to specify a date format and parse accordingly.
you have to split the string into tokens for month date and year and then create it using JS Date API.
var date="08.07.1987";
var newDate = date.replace(/(\.|-)/g,"/"));
var dateObject = new Date(newDate);
Replace the delimiters?
var dateStr = "08.07.1987",
dateObj = new Date(dateStr.replace(/[-.]/g,"/"));
Of course you can encapsulate that in a function if need be...
try this new Date("08.07.1987".replace('.','/','g')); tested on firefox only
I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.