How to get the correct datetime from momentjs - angularjs

I'm facing some difficulties to get the right datetime using the momentjs library.
I'm doing this
****--------- Datetime ----------****
Timestamp received from database -> 2016-07-12 17:21:40 <- THIS IS A UTC DATE TIME
moment.tz(datetime, moment.tz.guess()); //datetime value is '2016-07-12 17:21:40' and moment.tz.guess() is returning "America/Sao_Paulo"
// inspect the moment object returned
q{ _isAMomentObject:true, _i:"2016-07-12 17:21:40", _f:"YYYY-MM-DD HH:mm:ss", _isUTC:true, _pf:Object…}
_a: Array[7]
_d:Tue Jul 12 2016 14:21:40GMT-0300 (BRT) <- THIS IS THE CORRECT DATE TIME THAT I WANT
_f:"YYYY-MM-DD HH:mm:ss"
_i:"2016-07-12 17:21:40"
_isAMomentObject:true
_isUTC:true
_isValid:true
_locale:B
_offset:-180
_pf:Object
_z:h
__proto__:Object
// moment object formatted .format('YYYY-MM-DD HH:mm:ss')
2016-07-12 17:21:40 <- THIS IS THE DATE TIME THAT I'M GETTING, AND IT IS WRONG
****-------------------****
I don't know what I'm doing wrong here.

Solution:
I found out how to archive this.
moment.tz(moment.utc(datetime), moment.tz.guess());

Related

Change datepicker date format according to saved in database

sorry for the noob question but is it possible to change the date format in date picker?
I have searched in google but the only thing I see is either I change the CultureInfo or set the StringFormat.
I tried binding the StringFormat but I realized that it is not a DP.
So is it possible to change date format? Let's say:
Formats in database:
mm/dd/yyyy
dd/mm/yyyy
yyyy/dd/mm
yyyy/mm/dd
...(etc)
TIA!
This can help you..
Match the format first
(in the array, add more formats to check)
string[] formats = {"M/d/yyyy", "MM/dd/yyyy",
"d/M/yyyy", "dd/MM/yyyy",
"yyyy/M/d", "yyyy/MM/dd",
"M-d-yyyy", "MM-dd-yyyy",
"d-M-yyyy", "dd-MM-yyyy",
"yyyy-M-d", "yyyy-MM-dd",
"M.d.yyyy", "MM.dd.yyyy",
"d.M.yyyy", "dd.MM.yyyy",
"yyyy.M.d", "yyyy.MM.dd",
"M,d,yyyy", "MM,dd,yyyy",
"d,M,yyyy", "dd,MM,yyyy",
"yyyy,M,d", "yyyy,MM,dd",
"M d yyyy", "MM dd yyyy",
"d M yyyy", "dd MM yyyy",
"yyyy M d", "yyyy MM dd"
};
DateTime dateValue;
foreach (string dateStringFormat in formats)
{
if (DateTime.TryParseExact(sqlDateTime, dateStringFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
}
Then change it to your format
var x=dateValue.ToString("yyyy-MM-dd"); // or any other format

Format predefine date into MMM dd yyyy - HH:mm:ss in angular.js

I am getting date like "Tue Jan 19 18:25:08 +0000 2010".
I have to convert above date to MMM dd yyyy - HH:mm:ss or MM dd yyyy - HH:mm:ss format.
I tried and same created a Plunker for the reference. Below is link for the same
Date is formatting correctly but time is not formatting.
https://embed.plnkr.co/2YouE4gQLAuPCOOnvAJF/
I suggest to use moment and angular moment for date related stuff.
In the controller(create a moment object):
$scope.date = moment(<date>, 'ddd MMM DD HH:mm:ss Z YYYY'); //format for Tue Jan 19 18:25:08 +0000 2010
In the view(manipulate the moment object using angular moment):
<p data-ng-bind="date | amDateFormat : 'MMM dd yyyy - HH:mm:ss'"></p>
I always use moment for dates.
$scope.newDate = moment(yourCurrentDate).format('MM DD YYYY HH:mm:ss');
You can use this to convert into timestamp in milliseconds:
$scope.newDate = moment(yourCurrentDate).format('x');
If you want to display a timestamp in milliseconds in any format:
<span>{{newDate | date:'dd/MM/yyyy - hh:mm:ss'}}</span>

How to compare string data with real date

Need some help. Try compare string time with realt time.
<div ng-if="{{currentTime | date:'H:M'}} > {{stingtime}}">Hide</div>
Where currentTime is:
$scope.currentTime = new Date();
And {{stingtime}} is parsed from JSON time looks like 18:35
Finnaly I try compare time ex: 13:45 with string 18:45

Date format issue in Firefox browser

My code
for(n in data.values){
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}
here console.log shows perfect date in Chrome as 'Thu Aug 07 2014 14:29:00 GMT+0530 (India Standard Time)', but in Firefox it is showing as 'Invalid Date'.
If I console.log(data.values[n].snapshot) before the new Date line, it is showing date as
2014-08-07 14:29
How can I convert the date format to Firefox understandable way.
The Date object only officially accepts two formats:
Mon, 25 Dec 1995 13:30:00 GMT
2011-10-10T14:48:00
This means that your date 2014-08-07 14:29 is invalid.
Your date can be easily made compatible with the second date format though (assuming that date is yyyy-mm-dd hh:mm):
for(n in data.values){
n = n.replace(/\s/g, "T");
data.values[n].snapshot = new Date(data.values[n].snapshot);
data.values[n].value = parseInt(data.values[n].value);
console.log(data.values[n].snapshot);
}

Change Date value from one TimeZone to another TimeZone

my case is I have a Date obj the date inside is UTC time. However I want it to be changed to Japan time.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
calendar.setTime(someExistingDateObj);
System.out.println(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + calendar.get(Calendar.MINUTE));
the existingDateObj is mapped from db and db value is 2013-02-14 03:37:00.733
04:37
it seems the timezone is not working?
thanks for your time....
Your problem may be that you're looking at things wrong. A Date doesn't have a time zone. It represents a discrete moment in time and is "intended to reflect coordinated universal time". Calendars and date formatters are what get time zone information. Your second example with the Calendar and TimeZone instances appears to work fine. Right now, this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Japan"));
System.out.println(String.valueOf(calendar.get(Calendar.HOUR)) + ":" + calendar.get(Calendar.MINUTE));
}
Reports:
0:32
That appears correct to me. What do you find wrong with it?
Update: Oh, perhaps you're expecting 12:32 from the above code? You'd want to use Calendar.HOUR_OF_DAY instead of Calendar.HOUR for that, or else do some hour math. Calendar.HOUR uses 0 to represent both noon and midnight.
Update 2: Here's my final attempt to try to get this across. Try this code:
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("H:mm a Z");
List<TimeZone> zones = Arrays.asList(
TimeZone.getTimeZone("CST"),
TimeZone.getTimeZone("UTC"),
TimeZone.getTimeZone("Asia/Shanghai"),
TimeZone.getTimeZone("Japan"));
for (TimeZone zone : zones) {
calendar.setTimeZone(zone);
format.setTimeZone(zone);
System.out.println(
calendar.get(Calendar.HOUR_OF_DAY) + ":"
+ calendar.get(Calendar.MINUTE) + " "
+ (calendar.get(Calendar.AM_PM) == 0 ? "AM " : "PM ")
+ (calendar.get(Calendar.ZONE_OFFSET) / 1000 / 60 / 60));
System.out.println(format.format(calendar.getTime()));
}
}
Note that it creates a single Calendar object, representing "right now". Then it prints out the time represented by that calendar in four different time zones, using both the Calendar.get() method and a SimpleDateFormat to show that you get the same result both ways. The output of that right now is:
22:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
12:59 PM 8
12:59 PM +0800
13:59 PM 9
13:59 PM +0900
If you used Calendar.HOUR instead of Calendar.HOUR_OF_DAY, then you'd see this instead:
10:59 PM -6
22:59 PM -0600
4:59 AM 0
4:59 AM +0000
0:59 PM 8
12:59 PM +0800
1:59 PM 9
13:59 PM +0900
It correctly shows the current times in Central Standard Time (my time zone), UTC, Shanghai time, and Japan time, respectively, along with their time zone offsets. You can see that they all line up and have the correct offsets.
sdf2 and sdf3 are equaly initialized, so there is no need for two of them.

Resources