I am trying to pass a time stamp to my API that comes in the format of YYYY-MM-DD HH:MM:SS but i need to manipulate the time to add 5 hours.
Have I done something wrong here? Do I need to convert it to a JavaScript date first?
var manDate = "2020-08-16 16:15:00"
manDate.setHours(manDate.getHours() + 5);
data.manDate = manDate
console.log(manDate)
Expected output - 2020-08-16 21:15:00
When you create a var for date, you need to add the 'new Date()' method.
var manDate = new Date("2020-08-16 16:15:00");
manDate.setHours(manDate.getHours() + 5);
console.log(manDate.getHours());
And to log the hours use getHour() again in the log statement.
Use simpleDateFormat to format the date, then cast the formatted date to calendar and add hours to it.
Try with below code.
SimpleDateFormat sdfObj = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
sdfObj.parse("2020-08-16 16:15:00");
Calendar calendar = sdfObj.getCalendar();
calendar.add(Calendar.HOUR_OF_DAY, 5);
The question was asked to give a solution in java earlier. Below is the answer as per java.
Date newDate = DateUtils.addHours(oldDate, 5);
Related
I am looking for to convert this date and time to relative time like twitter have ex. 5 sec ago or 1 month ago.
I have stored date and time in different column of database.
i am executing this in JavaScript, i tried Momentjs but it does not work well or i may not understand its format or flow of work.
i am little new to javascript, i started learning react a week ago
from database:
date =>31.03.2019 time=>16:06:05
Thank You For Help.
Success Update :
Mission was to convert "31.3.2019 19:45:55" this date into a time like twitter or facebook have.
let date = new Date()
var then = request.date +' '+ request.time;
var ms = moment(date,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD.MM.YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
var timeAgo = d.humanize();;
In moment we can define the format of date we are passing and with humanize function its easy to get the desire output.
What i didnt know was the definition of format of date we pass. #NOW I AM CLEAR SIR.
Thank You For Helping Everybody. Thank You For Sharing .
You can use moment js
let date = new Date()
console.log(moment(date).fromNow())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
I've two javascript variables: startDate and endDate. I want to store the current time(unix timestamp in ms) to endDate and timestamp of 24 hours before in startDate. I'll use this two variables to query the records for last 1 day. How can I do that using moment JS?
Have you looked at the website yet? It's full of examples - http://momentjs.com/ I think what you are trying to do is as simple as
var startDate = moment(endDate).subtract(1, 'days');
Following your question more literally, you can do this:
var endDate = moment(); //the current time
Or, you can just ignore the endDate part of this problem and go straight to startDate with
var startDate = moment().subtract(1, 'days'); //one day before the current time
Finally, if you need to format it a certain way, you can apply a formatting rule such as:
moment().subtract(1,'days').format('YYYY-MM-DD h:mm:ss a')
Use format without an argument and it gives you ISO 8601 format
moment().subtract(1,'days').format() //eg: "2015-04-04T01:53:26-05:00"
This worked for me although I have never found it in the docs. Should have been published but it works.
Try:
moment(currentTime).format("hh:mm"));or
var currentTime = moment();
console.log("CURRENT TIME: " + moment(currentTime).format("hh:mm"));
For those who are looking for a way to get timestamp, just do it:
moment().valueOf()
I think what you are looking for is something like
moment(endDate).unix()
which returns something like:
1435161240
You can even calculate the time from now using
moment(endDate).fromNow()
which returns something like:
"2 days ago"
You can directly call function momentInstance.valueOf(), it will return numeric value of time similar to date.getTime() in native java script.
In my application I am getting date in a string format like :
var date="1988-11-4".
I am calling back the WCF service and sending data to the service as Json format. But my problem is the WCF service is only accepting the dates as {DoB:"/Date(570931200000+0530)/"} format.
can you please tell how do I convert date to json date format like:
var jasonDate="/Date(570931200000+0530)/". Where 570931200000 is the miliseconds calculated since from "1970-01-01" and +0530 is the Timezone.
As a best guess, and to give you something to work with, until you understand what the relationship is and come back and explain things better along with what you have tried and the precise nature of the problem with your code.
var dateTime = '1988-05-03',
parts = dateTime.split('-'),
date;
parts[1] -= 1;
date = new Date(Date.UTC.apply(null, parts));
document.body.textContent = '/Date(' + date.getTime() + '-0000)/';
This might work:
var jsonDate = new Date(date).toJSON();
As the initial variable is only a string it would not be recognised as a date so create a date from it then convert that to JSON.
Thank you all for your response. I have got solution to my query. Here in the string "/Date(1208559600000-0700)/" 1208559600000 is the milliseconds calculated since from Jan 01 1970 and -700 is the time zone.
This the code that worked for me:
convertToJsonDate: function (date) {
var diff = date.getTime();
var jsonDate = "\/Date(" + diff + "-0700)\/";
return jsonDate;
},
I know how to use new Date(UTCStrings) to local timezone.
But now, the question is how to convert an UTCString to other timezone (not local).
e.g.
The UTCString is '1338480000000'.
My local timezone is UTC+4.
I want to convert the date(UTCString) to UTC+9.
How can I do it?
Appreciate for your help!
Update
Thanks for BalaKrishnan's help.
I follow BalaKrishnan's key points maked a simple function. Hopefully, this will help others.
function utcToOtherTimezone(utcString, timezone){
var isoDt = new Date(utcString), // do this to convert it to iso time:
dt = isoDt.addMinutes( isoDt.getTimezoneOffset() + (timezone * 60) );
return dt.toLocaleDateString() + ' ' + dt.toLocaleTimeString();
}
$('#dtime').html(utcToOtherTimezone(1341282169000, +8));
And don't forget to add datejs
Online testing example http://jsfiddle.net/ysjia/FWbZ8/. Enjoy it.
First create a Date object from the UTCString as follows:
var utcString = 1338480000000;
// This will however be in local time, not iso time.
var isoDt = new Date(utcString);
// do this to convert it to iso time:
isoDt.addMinutes( isoDt.getTimezoneOffset()
);
// addMinutes is an API from Date.js.
Now the isoDt object has it's date value the same as the UTC date, to which you can add the necessary offset +9 or whatever.
Refer to this jquery faq that discusses this:
http://jqfaq.com/how-to-parse-a-date-string-disregarding-time-zones/
so, i need format JSON date from this format
"9/30/2010 12:00:00 AM", it is MM/DD/YYYY HH:MM:SS to format like this : DD/MM/YYYY, so i dont need info about hours, min and sec, and i need replace months and days from json, i tried some different ways but it always failed
i need do this using jQuery
also i didnt find any answer to formating this date type, all i found was formating date like this :/Date(1224043200000)/
so anyone have idea?
you can create a Date Object from a string like so:
var myDate = new Date(dateString);
then you can manipulate it anyway you want, one way to get your desired output is:
var output = myDate.getDate() + "\\" + (myDate.getMonth()+1) + "\\" + myDate.getFullYear();
you can find more at this elated.com article "working with dates"
Unfortunately your "from" dateformat is not the one which is implementation-independent in JavaScript. And all the other formats depends on the implementation, which means even if this format would be understood by most of the implementation I/you can't be sure for example how the DD and MM order would be parsed (I am almost sure it would be local regional settings dependent). So I would recommend to use a 3rd party (or your hand written) date parser to get a Date object out of your input string. One such parser you can find here:
http://www.mattkruse.com/javascript/date/
Because your question is not 100% clear for me, it's possible that you have your date in the format of /Date(number)/ which suggests that you are calling an ASP.Net service from your jQuery code. In this case during the JSON parse you can convert it to a Date object:
data = JSON.parse(data, function (key, value) {
// parsing MS serialized DateTime strings
if (key == '[NAME_OF_DATE_PROPERTY_IN_THE_JSON_STRING]') {
return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
// maybe new Date(parseInt(value.substr(6))) also works and it's simpler
}
return value;
});
The code below solved my problem:
var date = new Date(parseInt(d.data[i].dtOrderDate.replace("/Date(", "").replace(")/", ""), 10));
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
Try something like this :
var date = new Date(parseInt(jsonDate.substr(6)));
where jsonDate is variable that stores your date