Convert From Epoch to Date String - javascript

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);

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.

Convert string to Date without considering timezone - Typescript

I'm getting a date as a string, in the following format (for example):
"11/10/2015 10:00:00"
This is UTC time.
When I create Date from this string, it consider it as local time:
let time = "11/10/2015 10:00:00";
let date = new Date(time);
console.log(date);
it prints:
"Tue Nov 10 2015 10:00:00 GMT+0200"
(instead of considering it as UTC: "Tue Nov 10 2015 10:00:00")
I also tried moment.js for that.
is there a good way to make Date() consider the string a UTC, without adding "Z"/"UTC"/"+000" in the end of the string?
You can use the built-in Date.UTC() function to do this. Here's a little function that will take the format you gave in your original post and converts it to a UTC date string
let time = "11/10/2015 10:00:00";
function getUTCDate(dateString) {
// dateString format will be "MM/DD/YYYY HH:mm:ss"
var [date, time] = dateString.split(" ");
var [month, day, year] = date.split("/");
var [hours, minutes, seconds] = time.split(":");
// month is 0 indexed in Date operations, subtract 1 when converting string to Date object
return new Date(Date.UTC(year, month - 1, day, hours, minutes, seconds)).toUTCString();
}
console.log(getUTCDate(time));
Your date is parsed by the date constructor, in MM/DD/YYYY format, applying the local timezone offset (so the output represents local midnight at the start of the day in question). If your date really is MM/DD/YYYY, all you need to do is subtract the timezone offset and you'll have the UTC date...
var myDate = new Date("11/10/2015 10:00:00");
myDate = new Date( myDate.getTime() - (myDate.getTimezoneOffset()*60*1000));
console.log(myDate.toLocaleString([],{timeZone:'UTC'}))
Here's everything I know about managing timezone when serializing and deserializing dates. All the timezone handling is static! JS dates have no intrinsic timezone.
You can use Date.UTC for this but you will have to parse your string and put every part of it as args by yourself as it can't parse such string. Also you could use moment.js to parse it: Convert date to another timezone in JavaScript
Also, seems like new Date("11/10/2015 10:00:00 GMT") parses date as a GMT and only after that converts it to PC local time
Easy answer is to append a "Z" at the end without changing the variable:
let time = "11/10/2015 10:00:00";
let date = new Date(time + "Z");
console.log(date);

Convert systematic date to formatted date

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).

how get month from a string in javascript

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")));

Why 1387568966 is an invalid Date in Javascript?

This is my code:
var data = "1387568966 ";
var parsedDate = new Date(Date.parse(data));
but if I print parsedDate it says "Invalid Date".
Where am I wrong? It should works with timestamp.
To create a date with a timestamp use the Date constructor taking a number as argument (the number of milliseconds since Epoch) :
var data = "1387568966 ";
var parsedDate = new Date(data*1000); // converts from "seconds" to milliseconds
or
var parsedDate = new Date(parseFloat(data)*1000);
if you want to make your code more obvious.
You are using Data.parse() wrong, it does the opposite of what you think.
From MDN:
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.
Instead, just parse your string into an integer and pass it to the date constructor.

Categories

Resources