How to convert date into timestamp with javascript - javascript

I have a date in YYYY-MM-DD format and I want to convert it into a timestamp like 1645985084088 using javascript. Can someone help me out?

To ensure consistency regardless of the timezone of the user:
let date = '2022-03-02';
new Date(date + 'T00:00Z').getTime();
This will give the UTC timestamp for midnight at the start of the given date.

Using .getTime() you can covert a Date object to a timestamp in JavaScript:
let date = new Date("02-03-2022");
console.log(date.getTime());

Related

Luxon substracting one day if JS date is UTC

I create a date with Luxon in the following way:
const date = DateTime.utc(2000, 6, 23).toFormat('yyyy-MM-dd')
Then I try to convert it to a JS date by doing this:
const jsDate = new Date(date)
Finally, I convert it back to the 'yyyy-MM-dd' format with
const parsedDate = DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd')
But instead of giving me 2000-06-23 it gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
I try to convert it to a JS date by doing const jsDate = new Date(date)
Uh, why not just date.toJSDate()?
Instead of giving me 2000-06-23, DateTime.fromJSDate(jsDate).toFormat('yyyy-MM-dd') gives me 2000-06-22. Is this a bug on Luxon or I need to do something to get the correct date?
According to its docs, fromJSDate defaults to creating a DateTime instance in the local (system) timezone. If you expect another UTC date back, you need to specify that:
DateTime.fromJSDate(jsDate, {zone: 'utc'}).toFormat('yyyy-MM-dd')

Format date and time from UTC with offset using date-fns

I have a UTC time with offset like below. I'm trying to format the UTC date time string using format function from date-fns library.
import { format } from "date-fns";
const utcDateTime = "2021-10-14T21:03:56.3256046+00:00";
const formattedDate = format(new Date(utcDateTime), "MM/dd/yyyy hh:mm");
What I'm expecting is 10/14/2021 21:03, a 24 hour time format but what I get is 10/14/2021 04:03, a converted date time for my timezone.
How to display the date and time exactly like with UTC time instead of converting the date time to local timezone?
I created a working example using CodeSandbox. Could anyone please help?
After spending a lot of time, I was able to achieve the desired result using the plain JavaScript Date object and its functions.
First, parsing the date time string and converting it to ISO string using toISOString() function.
Second, splitting the formatted date and time extracts from the ISO string.
Below is the code
const formatToUTCDateTime = (dateString) => {
const date = new Date(Date.parse(dateString));
const formattedDate = date.toISOString().split("T")[0].split("-");
const formattedTime = date.toISOString().split("T")[1].split(":");
return `${formattedDate[1]}/${formattedDate[2]}/${formattedDate[0]} ${formattedTime[0]}:${formattedTime[1]}`;
};
console.log("Result - ", formatToUTCDateTime("2021-10-14T20:03:56.3256046+00:00"));

javascript -- Convert date string to another format?

I have date string like yyyy-MM-dd HH:mm:ss.SSS which is PST timezone, I need to change it to UTC format 2016-07-28T17:22:51.916Z. How can I do that?
This can be done pretty easily in JS:
var pstDate = new Date(myValidPstDateString);
// ISO is always in UTC time
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
console.log(pstDate.toISOString());

JavaScript date displaying the wrong day and time

I have this application where I want to use you date, but the problem is that the date is not working as I expect.
I create a date object like this:
// Get today's date
today: function () {
// Create a new date
var date = new Date();
// Set to midnight
date.setHours(0, 0, 0, 0);
// Return our date
return date;
},
and If I output that date in my view I get yesterdays date at 23:00 hours....
Which looks like this:
2015-07-08T23:00:00.000Z
Does anyone know how I can get the date to be formatted properly?
Update
Just to elaborate a bit, I want to use the date to compare against records in the database. These records have the date applied to them, because the JavaScript is showing the local date time, it is not comparing correctly. Also there is a case where I am saving that date and I don't want it to save the local date.
based on your culture setting you can use the
date.toLocaleDateString()
this will give localized string format back
date.toUTCString();
date.toLocaleString();
date.toLocaleDateString();
date.toDateString();
date.toISOString();
Find your answer here :) And the best option is to use momentjs http://momentjs.com/
So, I ended up creating this function:
// Converts a date to a timeStamp
this.convertToTimeStamp = function (dateTime) {
// Get just the date
var date = dateTime.toDateString();
// Get the timestamp
var timeStamp = Date.parse(date);
// Return our timeStamp
return timeStamp;
};
If my understanding is correct, that should create the same date no matter what timezone / locale you are in.

How can I compare a UTC timestamp date to the current date in Javascript?

I have a UTC timestamp that I need to compare with the current date. The current date is the date.. not the hour or the second. Just the date
So I need to compare a UTC stamp to that in Javascript. Can anybody help me out?
The Date type in javascript has the getTime() method witch returns the milliseconds since 1/1/1970. So is you get the time from both dates you can compare them.
var dDate = new Date();
var iMilSecs = dDate.getTime();

Categories

Resources