Convert date/time into timestamp - javascript

I want to convert following date and time, into a timestamp 13 digits. Note I use strict mode.
Date:
Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)
Time:
03:00
I tried to use Date.parse() but it was not working. What is the best way to do this?

You can use the getTime function:
console.log(new Date('Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)').getTime());

Try below code
console.log(new Date().getTime());

Create the date object, add time as you wish and then use the getTime() function:
"use strict";
var date = new Date('Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)');
date.setHours(parseInt('03'),[parseInt('00')]); //apply hours and minutes
console.log(date.toString());
console.log(date.getTime()); //use of getTime()

console.log(+new Date("Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)"))

Related

JavaScript Date Manipulation (Insert Current Time in old Date)

I want to ask a question. Can we insert current Time in old Date?
Like this is a old date "Fri Nov 19 2021 00:00:00 GMT+0500 (Pakistan Standard Time)" and I want to convert it to something like this "Fri Nov 19 2021 (current Time right now) GMT+0500 (Pakistan Standard Time)".
Thanks in advance.
You can try using the setHours method, as in the following example:
const saveTime = new Date();
console.log(saveTime);
Tue Nov 23 2021 06:24:29 GMT-0500 (Cuba Standard Time)
saveTime.setHours(20,35,43)
console.log(saveTime);
Tue Nov 23 2021 20:35:43 GMT-0500 (Cuba Standard Time)

How to build Time object using a date string as Local Time

All:
Thanks for help. I wonder how can I build a Date object using local time string, for example:
If I use new Date("2016-07-01"), what I want to build is
2016-07-01 00:00:00 GMT-0800 (Pacific Standard Time) (say I am in San Francisco),
but right now, it gives me something like
Thu Jun 30 2016 16:00:00 GMT-0800 (Pacific Standard Time)
Any idea?
To convert the default UTC time created from running new Date(dateString):
const MILLISECONDS_PER_MINUTE = 60000;
const utcDate = new Date('2015-01-01');
console.log(new Date(utcDate.getTime() + (utcDate.getTimezoneOffset() * MILLISECONDS_PER_MINUTE))); // Wed Jan 01 2014 00:00:00 GMT-0700 (MST)

Date format in javascript with time 00:00:00

How to get Date Format as
Fri Dec 03 2014 00:00:00 GMT+0530 (IST)
Todays date with time 00:00:00 in javascript ?
Any help appreciated
var myDate = new Date();
myDate.setHours(0,0,0);
alert(myDate);
jsfiddle http://jsfiddle.net/nrud7j8v/2
about javascript setHours() method http://www.w3schools.com/jsref/jsref_setHours...

Converting a date/time string to a date object

I have a string:
"2014-07-30T21:11:35.6300000"
but when I do
new Date(str)
It computes:
Wed Jul 30 2014 17:11:35 GMT-0400 (Eastern Daylight Time)
Q: How do I tell JavaScript not to calculate GMT -0400?
(new Date("2014-07-30T21:11:35.6300000")).toUTCString()

Javascript date parse error

I'm trying to convert string into epoch time in milliseconds using specs found on:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
For some reason the following snippet of codes returns March 13 instead of Feb 24, 2014.
Snippet 1:
alert(Date(Date.parse("2014-02-24T09:49:22.000-0800")));
Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)
Snippet 2:
alert(Date(Date.parse("2014-02-24")));
Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)
Is this some sort of timezone issue or what is the mistake that i have done ?
try new
alert(new Date(Date.parse("2014-02-24")))
Try this:
function parseDate(input) {
var parts = input.split('-');
return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}
or
console.log(new Date(Date.parse("2014-02-08T00:00:00Z")).toString());

Categories