JS代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//indexjs
//获取应用实例
var app = getApp()
Page({
data: {
motto: '示例小程序-获取当前地理位、速度',
userInfo: {},
hasLocation:false,
location:{}
},
//事件处理函数
bindViewTap: function() {
wxnavigateTo({
url: '/logs/logs'
})
},
onLoad: function () {
consolelog('onLoad')
var that = this
//调用应用实例的方法获取全局数据
appgetUserInfo(function(userInfo){
//更新数据
thatsetData({
userInfo:userInfo
})
}),
wxgetLocation( {
success: function( res ) {
consolelog( res )
thatsetData( {
hasLocation: true,
location: {
longitude: reslongitude,
latitude: reslatitude
}
})
}
})
}
})
indexwxml
1
2
3
4
5
6
7
8
9
10
11
12
<!--indexwxml-->
<view class="container">
<view bindtap="bindViewTap" class="userinfo">
<image class="userinfo-avatar" src="{{userInfoavatarUrl}}" background-size="cover"></image>
<text class="userinfo-nickname">{{userInfonickName}}</text>
</view>
<view class="usermotto">
<!-- <text class="user-motto">{{motto}}\n</text>-->
<text>经度:{{locationlongitude}}\n</text>
<text>纬度:{{locationlatitude}}</text>
</view>
</view>
运行效果
获取当前时间
首先,在要获取时间的js文件中加载utiljs文件
然后在onload方法中,调用utiljs中的formatTime方法获取当前时间
//获取当前时间
//
调用函数时,传入new
Date()参数,返回值是日期和时间
var
TIME
=
utilformatTime(new
Date());
thissetData({
time:
TIME,
});
这样就获取到了当前时间,但是我们发现在utiljs中并没有获取当前日期的方法。
没事,别慌!
这个时候我们去看下utiljs中获取当前时间的方法formatTime(),里面的方法写的特别容易理解,我们可以模仿这个方法写一个formatDate()方法。
看到那个小箭头了吗,你可以随便设置,我这样子设置后就是
2018-7-16
这样的效果。
到了这里,还差最后一步,如图
在moduleexports中一定要加上你写的方法,否则程序会报错说找不定formatDate这个方法。
加好了之后,就能成功在onload中调用这个方法获取日期了。
//获取当前日期
var
DATE
=
utilformatDate(new
Date());
thissetData({
date:
DATE,
});
以上所述是小编给大家介绍的微信小程序获取当前时间及日期的方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:微信小程序中时间戳和日期的相互转换问题微信小程序日期时间选择器使用方法微信小程序使用picker实现时间和日期选择框功能附源码下载微信小程序
滚动选择器(时间日期)详解及实例代码微信小程序之picker日期和时间选择器微信小程序
选择器(时间,日期,地区)实例详解
您好!很高兴能为您解答, 在微信小程序中,数据缓存其实就和localstorage 的原理差不多,所以理解起来并不难。下面我们来一起实现一下。
效果图展示:
我们在index页面存入数字11,然后在跳转到新页面,在将缓存中的11取出渲染到当前页面。具体代码如下:
index页面:
<span style="font-size:24px;">
<view class="btn-area">
<navigator url="/navigator/navigatortitle=我是navi">跳转到新的页面post情求</navigator>
<navigator url="/redirect/redirecttitle=我是red" redirect>跳转到当前页面</navigator>
</view>
</span>
<view>
<input style="border:2rpx solid red" placeholder="输入信息" bindinput="getInput" />
<button style="border:2rpx solid yellow" bindtap="saveInput">存入</button>
</view>1234567891012345678910
index的js:
//indexjs
//获取应用实例
var app = getApp()
Page({
data: {
storage:''
},
onLoad: function () {
var that = this
//获取输入值
getInput:function(e){
thissetData({
storage:edetailvalue
})
},
//存储输入值
saveInput:function(){
wxsetStorageSync('storage', thisdatastorage)
}
})
12345678910111213141516171819202122231234567891011121314151617181920212223
跳转页面:
<view>从存储中得到的数据:{{storage}}</view>11
跳转页面的js:
var app = getApp();
var that;
Page( {
data: {
storage:''
},
onLoad: function(options) {
that = this;
//获取存储信息
wxgetStorage({
key: 'storage',
success: function(res){
// success
thatsetData({
storage:resdata
})
}
})
}
})
以上就是关于微信小程序获取到位置坐标后,怎么获取具体地理位置信息全部的内容,包括:微信小程序获取到位置坐标后,怎么获取具体地理位置信息、详解微信小程序获取当前时间及日期的方法、微信小程序怎么进行数据缓存等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)