Convert systematic date to formatted date - javascript

I want to convert systematic date into readable date format. However when I pass systematic date as argument to date constructor I get Invalid date response. How to do this properly in order to display formatted date such as dd-mm-yyyy for GMT+2 ?
var date = message.date; // => 1466663308000
var dateObject = new Date(date);
console.log(dateObject);
Console output:
Invalid Date

You have to make sure the timestamp value is a number and not a string:
var date = message.date;
var dateObject = new Date(+date); // note the +
console.log(dateObject);
Once you've got a valid date, there are many other questions here about formatting dates.

I tried the code, it is absolutely correct.
I can get the correct date
var d=new Date(1466663308000);
document.write(d);
But I tried another way:
var x = "1466663308000";
var d=new Date(x);
document.write(d);
I got the "Invalid date", so I guess, message.date should be a string, please try to long(message.date).

Related

Get "Day" From This Formatted Timestamp In Javascript

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.

How to get date format in Angular-JS

My requirement is something different. I want to get the date format, not to format the date. Means I have a date string and now I want to get the date format of that date and apply it to the another date as a format.
Let me explain in brief with example:
var dateStr = "2015-06-06T12:00:00Z";
var d = new Date(dateStr);
here my date format is yyyy-MM-ddTHH:mm:ssZ you can see in dateStr object.
Now i will create another date and want to apply the same date-format to this new date.
var formatStr = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // want to get this from above date, not hard coded like this.
var newDate = $filter('date')(d, formatStr);
here you can see that i have hard coded the format string, which i don't want to do. Here this string should be come from the above d date/or dateStr String.
You can do it by using momment.js
http://momentjs.com/downloads/moment.js
van date=new Date(date);
var dateInFormate=moment(date);
var date=dateInFormate.format('yyyy-MM-ddTHH:mm:ssZ');
As #Rob said, there is doubt on the reliably for all formats. What you need is pre defined map with key being the format and value being its corresponding regular expression.
Now, create a function with input as dateStr and will return the format. Like
function getDateFormat(dateStr) {
var format = default_format;
// Check in map for format
// If you get the format in map, return that else return a default format.
return format;
}

Convert From Epoch to Date String

I'm trying to convert from epoch time (numeric) to a string date. I have these two values, from October of last year to March of this year: 1349064000000,1362114000000. But when I do Date(num), I get today's date returned for both.
You must use new Date(num).
Date(), without "new", doesn't create a new Date object, it only returns the current date as a string, regardless of any arguments you pass.
Try something like this:-
var date = new Date(1349064000000);
It will alert your wanted date.
var utcSeconds = 1349064000;
var d = new Date(0);
d.setUTCSeconds(utcSeconds);
alert(d);

Convert plain string date into required format and difference between two dates

I am getting date from XML but date is in plain string format. I would like to create the difference from today's date and time and the date and time which i am getting from xml.
For example i am getting date as a plain string in this format (2012-10-17T08:15:19.500-05:00).Now when i am doing difference with current date&time than i need to display something like this "2:hr,32min".
Any help/suggestion would be a great input.
Thanks
This should work:
var myDate = new Date( '2012-10-17T08:15:19.500-05:00' ),
newDate = new Date();
Browser results can vary when parsing dates. I have a test Fiddle here.
To get the difference between dates: var diff = myDate - newDate; and to convert that back to something useful: Convert time interval given in seconds into more human readable form

Jquery Date.parse returning NaN in Chrome browser?

I have a senario where i have to parse two dates for example start date and end date.
var startdate = '02/01/2011';
var enddate = '31/12/2011';
But if we alert start date
alert(Date.Parse(startdate)); i will get 1296498600000
but if i alert enddate
alert(Date.Parse(enddate)); i will get NaN
But this is working in other browsers except Chrome, But in other browsers
alert(Date.Parse(enddate)); i will get 1370889000000
Can anybody know a workaround for this?
If you want to parse a date without local differences, use the following, instead of Date.parse():
var enddate = '31/12/2011'; //DD/MM/YYYY
var split = enddate.split('/');
// Month is zero-indexed so subtract one from the month inside the constructor
var date = new Date(split[2], split[1] - 1, split[0]); //Y M D
var timestamp = date.getTime();
See also: Date
According to this
dateString
A string representing an RFC822 or ISO 8601 date.
I've tried your code and I also get NaN for the end date, but if i swap the date and month around, it works fine.

Categories

Resources