how can i extract time from datetime format.
my datetime format is given below.
var datetime =2000-01-01 01:00:00 UTC;
I only want to get the time 01.00 as 01
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
Update:
The new locales and options arguments let applications specify the
language whose formatting conventions should be used and customize the
behavior of the function. In older implementations, which ignore the
locales and options arguments, the locale used and the form of the
string returned are entirely implementation dependent.
// Depending on timezone, your results will vary
var event = new Date('August 19, 1975 23:15:30 GMT+00:00');
console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM
console.log(event.toLocaleTimeString('it-IT'));
// expected output: 01:15:30
What about these methods
new Date().getHours()
new Date().getMinutes()
For example:
var d = new Date();
var n = d.getHours();
Edited
Return the hour, according to universal time:
new Date().getUTCHours()
Example:
var d = new Date();
var n = d.getUTCHours();
As an alternative if you want to get the time from a string -
var datetime ="2000-01-01 01:00:00 UTC";
var myTime = datetime.substr(11, 2);
alert(myTime) //01
with the moment.js library it works in this way:
var result = moment().format('hh');
alert(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
var datetime = ("2000-01-01 01:00:00 UTC");
var d1 = new Date(datetime);
var minute = d1.getUTCMinutes();
var hour = d1.getUTCHours();
if(minute > 0)
alert(hour+"."+minute);
else
alert(hour);
Demo
var date1 = new Date(1945,10,20, 17,30)
var date2 = new Date(1970,1,8, 12,00)
console.log(date1.getHours() - 8 + (date1.getMinutes()/60))
console.log(date2.getHours() - 8 + (date2.getMinutes()/60))
Use the following code:
var datetime = "2000-01-01 01:00:00 UTC";
var dt = new Date(datetime);
var hr = dt.getUTCHours();
if(hr > 12) {
hr -= 12;
}
alert(hr);
refer this link also.
I have done it! It looks like this:
console.log(new Date().toLocaleTimeString() + " " + new Date().getSeconds() + " seconds");
Assuming you have a Date object like
var datetime = new Date("2000-01-01 01:00:00 UTC"); // might not parse correctly in every engine
// or
var datetime = new Date(Date.UTC(2000, 0, 1, 1, 0, 0));
then use the getUTCHours method:
datetime.getUTCHours(); // 1
I hope you can find a solution here.
let preDateTime = new Date("2022-03-31 22:26:00");
let newTime = preDateTime.toLocaleTimeString('en-US');
let hour = newTime.split(":")[0];
let amPm = newTime.split(" ")[1];
let seconds = newTime.split(":")[2].replace(amPm,'');;
let noAmPm = newTime.replace(amPm,'');
let noAmPmSeconds = noAmPm.replace(":"+seconds,'');
let noSeconds = newTime.replace(":"+seconds,' ');
if(parseInt(hour)<9){
newTime = "0"+newTime;
noAmPm = "0"+noAmPm
noSeconds= "0"+noSeconds
noAmPmSeconds = "0"+noAmPmSeconds;
}
console.log(newTime); //10:26:00 PM
console.log(noAmPm); //10:26:00
console.log(noSeconds); //10:26 PM
console.log(noAmPmSeconds); //10:26
Related
how can i extract time from datetime format.
my datetime format is given below.
var datetime =2000-01-01 01:00:00 UTC;
I only want to get the time 01.00 as 01
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
Date.prototype.toLocaleTimeString()
Returns a string with a locality sensitive representation of the time portion of this date based on system settings.
var time = datetime.toLocaleTimeString();
Update:
The new locales and options arguments let applications specify the
language whose formatting conventions should be used and customize the
behavior of the function. In older implementations, which ignore the
locales and options arguments, the locale used and the form of the
string returned are entirely implementation dependent.
// Depending on timezone, your results will vary
var event = new Date('August 19, 1975 23:15:30 GMT+00:00');
console.log(event.toLocaleTimeString('en-US'));
// expected output: 1:15:30 AM
console.log(event.toLocaleTimeString('it-IT'));
// expected output: 01:15:30
What about these methods
new Date().getHours()
new Date().getMinutes()
For example:
var d = new Date();
var n = d.getHours();
Edited
Return the hour, according to universal time:
new Date().getUTCHours()
Example:
var d = new Date();
var n = d.getUTCHours();
As an alternative if you want to get the time from a string -
var datetime ="2000-01-01 01:00:00 UTC";
var myTime = datetime.substr(11, 2);
alert(myTime) //01
with the moment.js library it works in this way:
var result = moment().format('hh');
alert(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
var datetime = ("2000-01-01 01:00:00 UTC");
var d1 = new Date(datetime);
var minute = d1.getUTCMinutes();
var hour = d1.getUTCHours();
if(minute > 0)
alert(hour+"."+minute);
else
alert(hour);
Demo
var date1 = new Date(1945,10,20, 17,30)
var date2 = new Date(1970,1,8, 12,00)
console.log(date1.getHours() - 8 + (date1.getMinutes()/60))
console.log(date2.getHours() - 8 + (date2.getMinutes()/60))
Use the following code:
var datetime = "2000-01-01 01:00:00 UTC";
var dt = new Date(datetime);
var hr = dt.getUTCHours();
if(hr > 12) {
hr -= 12;
}
alert(hr);
refer this link also.
I have done it! It looks like this:
console.log(new Date().toLocaleTimeString() + " " + new Date().getSeconds() + " seconds");
Assuming you have a Date object like
var datetime = new Date("2000-01-01 01:00:00 UTC"); // might not parse correctly in every engine
// or
var datetime = new Date(Date.UTC(2000, 0, 1, 1, 0, 0));
then use the getUTCHours method:
datetime.getUTCHours(); // 1
I hope you can find a solution here.
let preDateTime = new Date("2022-03-31 22:26:00");
let newTime = preDateTime.toLocaleTimeString('en-US');
let hour = newTime.split(":")[0];
let amPm = newTime.split(" ")[1];
let seconds = newTime.split(":")[2].replace(amPm,'');;
let noAmPm = newTime.replace(amPm,'');
let noAmPmSeconds = noAmPm.replace(":"+seconds,'');
let noSeconds = newTime.replace(":"+seconds,' ');
if(parseInt(hour)<9){
newTime = "0"+newTime;
noAmPm = "0"+noAmPm
noSeconds= "0"+noSeconds
noAmPmSeconds = "0"+noAmPmSeconds;
}
console.log(newTime); //10:26:00 PM
console.log(noAmPm); //10:26:00
console.log(noSeconds); //10:26 PM
console.log(noAmPmSeconds); //10:26
Hi, I would like to add 12 months and subtract 1 day for my current
date.
Example :
valStartDate :2018-01-20
expected_date:2019-01-19
I try below code but error "getFullYear() not a function to allow"
this.endDate =this.valStartDate.getFullYear()+1+'-'+this.valStartDate.getMonth()+'-'+(this.valStartDate.getDate()-1);
Ensure that your given start date is a date and not a string.
var startDate = new Date(2018, 0, 20);
var startDatePlus12Months = new Date(startDate.setMonth(startDate.getMonth() + 12));
var expectedDate = new Date(startDatePlus12Months.getFullYear(), startDatePlus12Months.getMonth(), startDatePlus12Months.getDate() - 1);
Here is a method of abstracting the date you want, apply this the variable and you should be good to go.
var date = new Date(); // now
var newDate = new Date(date.getFullYear() + 1, date.getMonth(), date.getDate() - 1);
console.log(newDate.toLocaleDateString());
this.valStartDate.getFullYear() In order for this to work, this.valStartDate must be a valid javascript date and look the same format as new Date(); would give you.
Fri Apr 26 2019 11:52:15 GMT+0100 (British Summer Time)
this.endDate = new Date(this.endDate); // <= maybe you get a string date...
this.endDate.setMonth(this.endDate.getMonth() + 12);
this.endDate.setDate(this.endDate.getDate() - 1);
If you're getting your date from a server or from a previous Json format, maybe you need to convert it from string to Date first: this.endDate = new Date(this.endDate);. It seems this is your case.
This is easy with the help of Moment.js:
const startDate = moment('2018-01-20');
const endDate = startDate.add(12, 'months').subtract(1, 'days').toDate();
I have another question on SO Unable to read date cell. This question is related to last question but more generic. How to convert Raw date, which represents number of days since 1st Jan 1900, to a javascript date type? [ Forget office365 ].
I have number of days elapsed since 1st Jan 1900. How can I get the date from it. For ex: I need a date after 42216 days, since 1st Jan 1900, How can I calculate that date? Answer is : 31-Jul-2015.
Try this:
(function(){
var date = new Date(1900,1,1);
var dayCount = 42216;
date.setDate(date.getDate() + dayCount)
console.log(date);
})()
Try this:
start = "01/01/1900"
newDate = start.split("/");
x = new Date(newDate[2]+"/"+newDate[1]+"/"+newDate[0]);
var numberOfDaysToAdd = 42216;
x.setDate(x.getDate() + parseInt(numberOfDaysToAdd));
var dd = x.getDate();
var mm = x.getMonth() + 1;
var yyyy = x.getFullYear();
var format = dd+'/'+mm+'/'+yyyy;
alert(format);
JSFIDDLE DEMO
Hope it help:
var dateStart= new Date('1900-01-01');
var afterDay=42216;
var newDay=new Date(dateStart.getTime() + afterDay*24*60*60*1000);
alert(newDay);
I'm looking for something like yyyy/MM/dd hh:mm:ss ffff
Date.now() returns the total of milliseconds (ex: 1431308705117).
How can I do this?
You can use the Date constructor which takes in a number of milliseconds and converts it to a JavaScript date:
var d = new Date(Date.now());
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
In reality, however, doing Date(Date.now()) does the same thing as Date(), so you really only have to do this:
var d = new Date();
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
You can use native JavaScript Date methods to achieve that or you can use a library like Moment.js.
It is a simple as:
moment().format('YYYY/MM/D hh:mm:ss SSS')
If you are going use a lot of date formatting/parsing in your application then I definitely recommend using it.
You can use Date().toISOString(), i.e.:
let d = new Date().toISOString();
document.write(d);
Output:
2022-02-04T17:46:16.100Z
Demo:
let d = new Date().toISOString();
document.write(d);
Simple
const DateNow = Date.now(); // 1602710690936
console.log(new Date(DateNow).toString()) // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
function formatted_date()
{
var result="";
var d = new Date();
result += d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate() +
" "+ d.getHours()+":"+d.getMinutes()+":"+
d.getSeconds()+" "+d.getMilliseconds();
return result;
}
console.log(formatted_date())
Output: "2015/5/10 22:5:26 429"
function millisecondsToHuman(ms) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / 1000 / 60) % 60);
const hours = Math.floor(ms / 1000 / 60 / 60);
const humanized = [
pad(hours.toString(), 2),
pad(minutes.toString(), 2),
pad(seconds.toString(), 2),
].join(':');
return humanized;
}
function pad(numberString, size) {
let padded = numberString;
while (padded.length < size) padded = `0${padded}`;
return padded;
}
Step 1: use new Date() to get the date as JavaScript format as Sun Jul 12 2020 15:40:16 GMT+0800 (Singapore Standard Time)
var d = new Date()
Step 2: use .toString() to convert to string and .substr string method to convert the previous string to "Jul 12 2020" and get rid of the rest
var d2 = d.toString().substr(4, 11)
Step 3: use .slice method to add '/' between dat, month and year to get Jul / 12 / 2020
var d3 = d2.slice(0, 3) + ' /' + d2.slice(3, 6) + ' /' + d2.slice(6))
const formattedDate = () => {
d = new Date()
cd = num => num.toString().padStart(2, 0)
return d.getFullYear()+"/"+cd(d.getMonth() + 1)+"/"+cd(d.getDate()) +
" "+ cd(d.getHours())+":"+cd(d.getMinutes())+":"+
cd(d.getSeconds())+" "+d.getMilliseconds()
}
console.log(formattedDate) //returns "2022/11/01 03:00:36 777"
var date = new Date();
will get you an answer formatted like this: Sun May 10 2015 21:55:01 GMT-0400 (Eastern Daylight Time)
var d = new Date();
var n = d.toJSON();
will get you the answer formatted the way you were looking for it.
Here is a great explanation of all the ways to manipulate the Date object
I like the dataformat package:
you can install using:
npm i dataformat.
and you can use like that:
dateFormat(medicao.DataHora, 'UTC:HH:MM')
I want to make a js date object based on strings in format YYYYMMDD and HHMMSS.
function makeTimeStamp(myDate, MyTime){
// myDate format YYYYMMDD
// myTime format HHMMSS
var YYYY = myDate.substring(0, 3);
var MM = myDate.substring(4, 5);
var DD = myDate.substring(6,7);
var HH = myTime.substring(0,1);
var MM = myTime.substring(2,3);
var SS = myTime.substring(4,5);
jsDate =
}
If you meant to be creating a Date object for given year, month, day, hours, minutes and seconds, you can use new Date(year, month - 1, day, hours, minutes, seconds) to create a Date instance for it. Note that the month is 0 based, January is 0, February 1, etc. Hence the MM - 1;
function stringToDate(myDate, myTime){
// myDate format YYYYMMDD
// myTime format HHMMSS
var YYYY = myDate.substr(0, 4);
var MM = myDate.substr(4, 2);
var DD = myDate.substr(6, 2);
var HH = myTime.substr(0, 2);
var mm = myTime.substr(2, 2);
var SS = myTime.substr(4, 2);
var jsDate = new Date(YYYY, MM - 1, DD, HH, mm, SS);
return jsDate;
}
// returns a Date object for given date and time
var jsDate = stringToDate("20110818", "191500");
// returns 1313687700000
var timestamp = jsDate.getTime();
Note that I'm using string.substr(start_position, length) as you can easily see the length of the returned value.
You can try Date.js which is a date library. http://www.datejs.com/
You can convert those date and time formats in to ISO format pretty easily (e.g. "2011-08-18T12:34:56") and use a regular expression to make sure the arguments are formatted properly:
function makeTimestamp(myDate, myTime) {
var ds = myDate.match(/^(\d{4})(\d\d)(\d\d)$/)
, ts = myTime.match(/^(\d\d)(\d\d)(\d\d)$/);
if (!ds || !ts) { return null; }
return new Date(ds.slice(1,4).join("-") + "T" + ts.slice(1,4).join(":"));
}
var d = makeTimestamp('20110818', '123456');
d // => Fri Aug 19 2011 12:34:56 GMT-0400 (EDT)
you can try this: https://www.npmjs.com/package/timesolver
npm i timesolver
use it in your code:
const timeSolver = require('timeSolver');
const date = new Date();
const dateString = timeSolver.getString(date, 'YYYYMMDD HHMMSS');
`
You can get date string by using this method:
const dateString = timeSolver.getString(date, format);
Hope this will help you!