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)
Related
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 can I get Fri Oct 25 2019 from a date object as below?
Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time)
Use the toDateString() function from JavaScript.
let d = new Date();
// note : the actual display output depend on your browser/system settings (locale)
console.log(d.toString());
console.log(d.toDateString());
This will take the first "date only" part of the string that is displayed from the date object.
It could also be done with string manipulation, but this function is actually intended for this usecase, so probably clearer.
Doc for toDateString function
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)"))
I'm trying to subtract 1 hour from the current date, I'm using this code:
var date = new Date();
// date is **Fri Apr 03 2015 16:47:33 GMT+0100 (GMT Standard Time)**
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1)).toISOString();
//now date is **Fri Apr 03 2015 14:47:33 GMT+0100 (GMT Standard Time)**
There's less two hours instead of just one. What am I missing here?
Your code works when looking at the elements in the same format.
<div class="original"></div>
<div class="updated"></div>
Using jQuery to post the information ... with your code, as is.
$(".original").text(date.toISOString());
$(".updated").text(uploadDateFilter);
Results in ...
2015-04-03T16:00:23.441Z
2015-04-03T15:00:23.441Z
http://jsfiddle.net/rfornal/5nma5uhr/
You don't need .toISOString(), but for me it works :
var date = new Date();
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1));
console.log(date);
console.log(uploadDateFilter);
Console output :
Fri, 03 Apr 2015 15:59:48 GMT
Fri, 03 Apr 2015 14:59:48 GMT
http://repl.it/gxL
The reason appears to be daylight saving check this my fiddle.
For me in the UK the clocks went forward on Mar 29, 2015. Checking the effect of new Date().toISOString() before and after this date, during daylight saving the result is one hour less.
I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.